Submission #2178671
Source Code Expand
#include <algorithm>
#include <cstdio>
#define LL long long
struct node{
int u,v;
LL w;
}g[400005];
int n,m,fa[200005],num;
LL d[200005],tot;
void add(int x,int y,int z)
{
g[++num].u=x; g[num].v=y; g[num].w=z;
return;
}
LL min(LL x,LL y)
{
return x<y?x:y;
}
bool cmp(node x,node y)
{
return x.w<y.w;
}
int find(int x)
{
if (fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void Kruskal()
{
int i,j,k,fu,fv,cnt;
for (i=0;i<n;i++)
fa[i]=i;
std::sort(g+1,g+num+1,cmp);
for (i=1;i<=num;i++)
{
fu=find(g[i].u);
fv=find(g[i].v);
if (fu!=fv)
{
fa[fv]=fu;
cnt++;
tot+=g[i].w;
if (cnt==n-1) return;
}
}
return;
}
int main()
{
int i,j,k,x,y;
LL z;
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
d[i]=2e9;
for (i=1;i<=m;i++)
{
scanf("%d%d%lld",&x,&y,&z);
add(x,y,z);
if (z+1<d[x]) d[x]=z+1;
if (z+2<d[y]) d[y]=z+2;
}
for (k=1;k<=2;k++)
for (i=0;i<n;i++)
d[i]=min(d[i],d[(i+n-1)%n]+2);
for (i=0;i<n;i++)
add(i,(i+1)%n,d[i]);
Kruskal();
// printf("%d\n",num);
printf("%lld",tot);
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
vjudge4 |
Language |
Bash (GNU bash v4.3.11) |
Score |
0 |
Code Size |
1057 Byte |
Status |
RE |
Exec Time |
387 ms |
Memory |
1516 KB |
Judge Result
Set Name |
sample |
all |
Score / Max Score |
0 / 0 |
0 / 1300 |
Status |
|
|
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 |
RE |
387 ms |
1516 KB |
01-02.txt |
RE |
4 ms |
588 KB |
01-03.txt |
RE |
4 ms |
588 KB |
01-04.txt |
RE |
4 ms |
588 KB |
01-05.txt |
RE |
4 ms |
588 KB |
01-06.txt |
RE |
4 ms |
588 KB |
01-07.txt |
RE |
4 ms |
596 KB |
01-08.txt |
RE |
4 ms |
588 KB |
01-09.txt |
RE |
4 ms |
596 KB |
01-10.txt |
RE |
4 ms |
588 KB |
01-11.txt |
RE |
4 ms |
588 KB |
01-12.txt |
RE |
4 ms |
588 KB |
01-13.txt |
RE |
4 ms |
588 KB |
01-14.txt |
RE |
4 ms |
588 KB |
01-15.txt |
RE |
4 ms |
588 KB |
01-16.txt |
RE |
4 ms |
588 KB |
01-17.txt |
RE |
4 ms |
588 KB |
01-18.txt |
RE |
4 ms |
588 KB |
01-19.txt |
RE |
4 ms |
588 KB |
01-20.txt |
RE |
4 ms |
596 KB |
01-21.txt |
RE |
4 ms |
588 KB |
01-22.txt |
RE |
4 ms |
584 KB |
01-23.txt |
RE |
4 ms |
596 KB |
01-24.txt |
RE |
4 ms |
588 KB |
01-25.txt |
RE |
4 ms |
588 KB |
01-26.txt |
RE |
4 ms |
588 KB |
01-27.txt |
RE |
4 ms |
588 KB |
01-28.txt |
RE |
4 ms |
596 KB |
01-29.txt |
RE |
4 ms |
588 KB |
01-30.txt |
RE |
4 ms |
596 KB |
sample-01.txt |
RE |
4 ms |
588 KB |
sample-02.txt |
RE |
4 ms |
596 KB |
sample-03.txt |
RE |
4 ms |
588 KB |