Submission #4420107
Source Code Expand
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<map> #include<iostream> using namespace std; #define ll long long #define re register #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout) inline int gi() { int f=1,sum=0;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum; } const int N=400010; int val[N],n,q,cnt,f[N]; ll ans; struct node { int u,v,w; bool operator<(const node&b)const { return w<b.w; } }e[N<<1]; int find(int x) { if(f[x]!=x)f[x]=find(f[x]); return f[x]; } int main() { n=gi();q=gi(); memset(val,127,sizeof(val)); for(int i=1;i<=q;i++) { int a=gi(),b=gi(),c=gi(); e[++cnt]=(node){a,b,c}; val[a]=min(val[a],c+1); val[b]=min(val[b],c+2); } int pos=0; for(int i=1;i<n;i++)if(val[pos]>val[i])pos=i; for(int i=(pos+1)%n,j=pos;i!=pos;j=i,i=(i+1)%n) val[i]=min(val[i],val[j]+2); for(int i=0;i<n-1;i++) e[++cnt]=(node){i,i+1,val[i]}; e[++cnt]=(node){n-1,0,val[n-1]}; sort(e+1,e+cnt+1); for(int i=0;i<n;i++)f[i]=i; int k=0; for(int i=1;i<=cnt;i++) { int u=e[i].u,v=e[i].v; if(find(u)!=find(v)) { ans+=e[i].w,f[find(u)]=find(v); k++; if(k==n-1)break; } } printf("%lld\n",ans); return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | vjudge2 |
Language | C++14 (GCC 5.4.1) |
Score | 1300 |
Code Size | 1365 Byte |
Status | AC |
Exec Time | 72 ms |
Memory | 11008 KB |
Judge Result
Set Name | sample | all | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1300 / 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 | AC | 2 ms | 4608 KB |
01-02.txt | AC | 38 ms | 8704 KB |
01-03.txt | AC | 70 ms | 11008 KB |
01-04.txt | AC | 22 ms | 11008 KB |
01-05.txt | AC | 14 ms | 9728 KB |
01-06.txt | AC | 19 ms | 10240 KB |
01-07.txt | AC | 17 ms | 8960 KB |
01-08.txt | AC | 18 ms | 8832 KB |
01-09.txt | AC | 21 ms | 8704 KB |
01-10.txt | AC | 44 ms | 8704 KB |
01-11.txt | AC | 60 ms | 8704 KB |
01-12.txt | AC | 70 ms | 10752 KB |
01-13.txt | AC | 70 ms | 10752 KB |
01-14.txt | AC | 70 ms | 10752 KB |
01-15.txt | AC | 72 ms | 10752 KB |
01-16.txt | AC | 70 ms | 10752 KB |
01-17.txt | AC | 70 ms | 10752 KB |
01-18.txt | AC | 52 ms | 10752 KB |
01-19.txt | AC | 17 ms | 8960 KB |
01-20.txt | AC | 20 ms | 8704 KB |
01-21.txt | AC | 33 ms | 8704 KB |
01-22.txt | AC | 56 ms | 10752 KB |
01-23.txt | AC | 57 ms | 10752 KB |
01-24.txt | AC | 14 ms | 8704 KB |
01-25.txt | AC | 72 ms | 10752 KB |
01-26.txt | AC | 21 ms | 8704 KB |
01-27.txt | AC | 22 ms | 8704 KB |
01-28.txt | AC | 48 ms | 8704 KB |
01-29.txt | AC | 57 ms | 8704 KB |
01-30.txt | AC | 68 ms | 10752 KB |
sample-01.txt | AC | 2 ms | 4608 KB |
sample-02.txt | AC | 2 ms | 4608 KB |
sample-03.txt | AC | 2 ms | 4608 KB |