Submission #2166397


Source Code Expand

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ll
{
	int x,y;
	long long v;
}b[600000];
long long ans=0,n,q,a[300000],t=0,f[300000];
int cmp(ll a,ll b)
{
	return a.v<b.v;
}
int find(int x)
{
	if (x==f[x])
	return x;
	return f[x]=find(f[x]);
}
void un(int x,int y)
{
	int fx=find(x),fy=find(y);
	f[fx]=fy;
}
void add(int x,int y,int v)
{
	b[++t].x=x;
	b[t].y=y;
	b[t].v=v;
}
int main()
{
    scanf("%d%d",&n,&q);
	int x,y;
	long long v;
	for (int i=0;i<=n;++i)
	a[i]=1e17;
	for (int i=1;i<=q;++i)
	{
		scanf("%d%d%lld",&x,&y,&v);
	    add(x,y,v);
	    a[x]=min(a[x],v+1);
		a[y]=min(a[y],v+2); 
	}	
	for (int i=1;i<=2;++i)
	for (int j=1;j<=n;++j)
	a[(j%n)]=min(a[(j%n)],a[j-1]+2);
	for (int i=0;i<n;++i)
	add(i,(i+1)%n,a[i]);
	sort(b+1,b+t+1,cmp);
	for (int i=0;i<n;++i)
	f[i]=i;
	for (int i=1;i<=t;++i)
	if (find(b[i].x)!=find(b[i].y))
	{
		un(b[i].x,b[i].y);
		ans+=b[i].v;
		if (t==n-1)
		break;
	}
	printf("%lld\n",ans);
} 

Submission Info

Submission Time
Task G - Zigzag MST
User vjudge3
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 965 Byte
Status AC
Exec Time 123 ms
Memory 15104 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:34:23: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘long long int*’ [-Wformat=]
     scanf("%d%d",&n,&q);
                       ^
./Main.cpp:34:23: warning: format ‘%d’ expects argument of type ‘int*’, but argument 3 has type ‘long long int*’ [-Wformat=]
./Main.cpp:34:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&q);
                        ^
./Main.cpp:41:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%lld",&x,&y,&v);
                             ^

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 36
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 4224 KB
01-02.txt AC 70 ms 6272 KB
01-03.txt AC 115 ms 12288 KB
01-04.txt AC 37 ms 10240 KB
01-05.txt AC 26 ms 8960 KB
01-06.txt AC 31 ms 9344 KB
01-07.txt AC 26 ms 8192 KB
01-08.txt AC 26 ms 8064 KB
01-09.txt AC 32 ms 7936 KB
01-10.txt AC 72 ms 9984 KB
01-11.txt AC 97 ms 9088 KB
01-12.txt AC 114 ms 11904 KB
01-13.txt AC 116 ms 11904 KB
01-14.txt AC 115 ms 12032 KB
01-15.txt AC 113 ms 12032 KB
01-16.txt AC 113 ms 12032 KB
01-17.txt AC 112 ms 12032 KB
01-18.txt AC 89 ms 15104 KB
01-19.txt AC 26 ms 8192 KB
01-20.txt AC 29 ms 7808 KB
01-21.txt AC 53 ms 9856 KB
01-22.txt AC 99 ms 11904 KB
01-23.txt AC 101 ms 11904 KB
01-24.txt AC 28 ms 11008 KB
01-25.txt AC 123 ms 15104 KB
01-26.txt AC 37 ms 10496 KB
01-27.txt AC 35 ms 9472 KB
01-28.txt AC 78 ms 11520 KB
01-29.txt AC 93 ms 10112 KB
01-30.txt AC 109 ms 13568 KB
sample-01.txt AC 3 ms 4224 KB
sample-02.txt AC 1 ms 4224 KB
sample-03.txt AC 1 ms 4224 KB