Submission #4419533
Source Code Expand
#include<bits/stdc++.h> using namespace std; #define Set(a,b) memset(a,b,sizeof(a)) template<class T>inline void init(T&x){ x=0;char ch=getchar();bool t=0; for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') t=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+(ch-48); if(t) x=-x;return; } typedef long long ll; int n,m; const int N=5e5+10; struct edge{ int u,v;ll val; inline bool operator <(const edge B)const{return val<B.val;} }a[N];int cnt=0; int q,fa[N]; int find(int x){return fa[x]==x? x:fa[x]=find(fa[x]);} ll data[N]; int main() { init(n),init(q);Set(data,-1); if(n==1) return puts("0"),0; for(int i=1;i<=q;++i){ int X,Y;ll Z; init(X),init(Y),init(Z); a[++cnt]=(edge){X,Y,Z}; if(~data[X]) data[X]=min(data[X],Z+1); else data[X]=Z+1; if(~data[Y]) data[Y]=min(data[Y],Z+2); else data[Y]=Z+2; } int pos=-1; for(int i=0;i<n;++i) {if(~data[i]) {if((~pos)&&data[pos]>data[i])pos=i;else if(pos==-1) pos=i;}} int u=pos,v; for(int i=1;i<=n;u=v,++i) { v=u+1;if(v>=n) v-=n; if(data[u]==-1) continue; if(~data[v]) data[v]=min(data[v],data[u]+2); else data[v]=data[u]+2; if(~data[u]) a[++cnt]=(edge){u,v,data[u]}; } sort(a+1,a+1+cnt); for(int i=0;i<n;++i) fa[i]=i; ll tot=0; for(int i=1;i<=cnt;++i) { u=a[i].u,v=a[i].v; int fx=find(u),fy=find(v); if(fx==fy) continue; else fa[fx]=fy,tot+=a[i].val; } cout<<tot<<endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | Neos |
Language | C++ (GCC 5.4.1) |
Score | 1300 |
Code Size | 1379 Byte |
Status | AC |
Exec Time | 81 ms |
Memory | 15616 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 | 4 ms | 6400 KB |
01-02.txt | AC | 43 ms | 10496 KB |
01-03.txt | AC | 78 ms | 12800 KB |
01-04.txt | AC | 20 ms | 12800 KB |
01-05.txt | AC | 15 ms | 11520 KB |
01-06.txt | AC | 29 ms | 12032 KB |
01-07.txt | AC | 16 ms | 10880 KB |
01-08.txt | AC | 17 ms | 10624 KB |
01-09.txt | AC | 21 ms | 10496 KB |
01-10.txt | AC | 50 ms | 12544 KB |
01-11.txt | AC | 66 ms | 12544 KB |
01-12.txt | AC | 78 ms | 12544 KB |
01-13.txt | AC | 78 ms | 12544 KB |
01-14.txt | AC | 79 ms | 12544 KB |
01-15.txt | AC | 79 ms | 12544 KB |
01-16.txt | AC | 78 ms | 12544 KB |
01-17.txt | AC | 77 ms | 12544 KB |
01-18.txt | AC | 51 ms | 15616 KB |
01-19.txt | AC | 15 ms | 10880 KB |
01-20.txt | AC | 19 ms | 10496 KB |
01-21.txt | AC | 34 ms | 10496 KB |
01-22.txt | AC | 59 ms | 12544 KB |
01-23.txt | AC | 60 ms | 12544 KB |
01-24.txt | AC | 15 ms | 13568 KB |
01-25.txt | AC | 81 ms | 15616 KB |
01-26.txt | AC | 27 ms | 13312 KB |
01-27.txt | AC | 25 ms | 12032 KB |
01-28.txt | AC | 51 ms | 14080 KB |
01-29.txt | AC | 61 ms | 13440 KB |
01-30.txt | AC | 73 ms | 14080 KB |
sample-01.txt | AC | 4 ms | 6400 KB |
sample-02.txt | AC | 3 ms | 6400 KB |
sample-03.txt | AC | 3 ms | 6400 KB |