Submission #3435769


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
template<typename T>inline bool chkmin(T &x,T y){return (y<x)?(x=y,1):0;}
template<typename T>inline bool chkmax(T &x,T y){return (y>x)?(x=y,1):0;}
inline int read(){
	int x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x*f;
}
inline ll readll(){
	ll x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
	return x*f;
}
const int maxn=2e5+10,inf=0x3f3f3f3f;
int n,m,Min[maxn],fa[maxn];
struct Edge{
	int x,y,z;
	bool operator <(const Edge &rhs) const{
		return z<rhs.z;
	}
}E[maxn<<1];
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
int main(){
	memset(Min,inf,sizeof(Min));
	n=read(),m=read();
	REP(i,1,m){
		E[i].x=read(),E[i].y=read(),E[i].z=read();
		chkmin(Min[E[i].x],E[i].z+1);
		chkmin(Min[E[i].y],E[i].z+2);
	}
	int cur=inf;
	REP(i,0,2*n){
		cur+=2;
		chkmin(Min[i%n],cur);
		chkmin(cur,Min[i%n]);
	}
	REP(i,0,n-1) E[++m]=(Edge){i,(i+1)%n,Min[i]};
	sort(E+1,E+m+1);
	REP(i,0,n-1) fa[i]=i;
	ll ans=0;
	REP(i,1,m){
		int x=E[i].x,y=E[i].y,z=E[i].z;
		if(find(x)==find(y)) continue;
		fa[find(x)]=find(y);
		ans+=z;
	}
	printf("%lld\n",ans);
	return 0;
}

Submission Info

Submission Time
Task G - Zigzag MST
User zhou888
Language C++14 (GCC 5.4.1)
Score 1300
Code Size 1608 Byte
Status AC
Exec Time 79 ms
Memory 9600 KB

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 2 ms 2432 KB
01-02.txt AC 42 ms 6528 KB
01-03.txt AC 75 ms 6784 KB
01-04.txt AC 21 ms 8832 KB
01-05.txt AC 13 ms 7552 KB
01-06.txt AC 18 ms 8064 KB
01-07.txt AC 16 ms 6912 KB
01-08.txt AC 16 ms 6656 KB
01-09.txt AC 21 ms 6528 KB
01-10.txt AC 48 ms 6528 KB
01-11.txt AC 64 ms 6528 KB
01-12.txt AC 75 ms 6528 KB
01-13.txt AC 76 ms 6528 KB
01-14.txt AC 76 ms 6528 KB
01-15.txt AC 79 ms 6528 KB
01-16.txt AC 75 ms 6528 KB
01-17.txt AC 77 ms 6528 KB
01-18.txt AC 53 ms 9600 KB
01-19.txt AC 16 ms 6784 KB
01-20.txt AC 19 ms 6528 KB
01-21.txt AC 34 ms 6528 KB
01-22.txt AC 59 ms 6528 KB
01-23.txt AC 58 ms 6528 KB
01-24.txt AC 15 ms 9600 KB
01-25.txt AC 74 ms 9600 KB
01-26.txt AC 21 ms 9088 KB
01-27.txt AC 22 ms 8064 KB
01-28.txt AC 50 ms 8064 KB
01-29.txt AC 60 ms 7424 KB
01-30.txt AC 70 ms 8064 KB
sample-01.txt AC 1 ms 2432 KB
sample-02.txt AC 1 ms 2432 KB
sample-03.txt AC 1 ms 2432 KB