Submission #3414946
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
namespace IO
{
char ibuf[(1<<21)+1],obuf[(1<<21)+1],st[15],*iS,*iT,*oS=obuf,*oT=obuf+(1<<21);
inline char Get()
{
return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++);
}
inline void Flush()
{
fwrite(obuf,1,oS-obuf,stdout);
oS=obuf;
}
inline void Put(char x)
{
*oS++=x;
if(oS==oT)
Flush();
}
inline int read()
{
register int x=0;
register char ch=Get();
while(ch>'9'||ch<'0')
ch=Get();
while(ch>='0'&&ch<='9')
x=(x<<3)+(x<<1)+(ch^48),ch=Get();
return x;
}
inline void write(int x)
{
register int top=0;
while(x)
st[++top]=(x%10)+48,x/=10;
while(top)
Put(st[top--]);
}
}
using namespace IO;
const int N=200001,M=N<<3;
struct Edge
{
int u,v,w,Next;
Edge(){}
Edge(int U,int V,int W,int NEXT)
{
u=U,v=V,w=W,Next=NEXT;
}
bool operator<(Edge a)const
{
return w<a.w;
}
}edge[M];
int head[N],tot,f[N],d[N];
inline void add(int x,int y,int z)
{
edge[++tot]=Edge(x,y,z,head[x]),head[x]=tot;
}
int find(int x)
{
return x==f[x]? x:f[x]=find(f[x]);
}
bool cmp(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy)
return false;
if(fy>fx)
f[fy]=fx;
else
f[fx]=fy;
return true;
}
int main()
{
register int n=read(),q=read();
memset(d,0x3f,sizeof d);
for(register int i=1,a,b,c;i<=q;++i)
{
a=read(),b=read(),c=read();
add(a,b,c);
d[a]=min(d[a],c+1),d[b]=min(d[b],c+2);
}
for(register int i=0;i<n;++i)
d[i]=min(d[i],d[(i-1+n)%n]+2);
for(register int i=0;i<n;++i)
d[i]=min(d[i],d[(i-1+n)%n]+2);
for(register int i=0;i<=n;++i)
add(i,(i+1)%n,d[i]);
sort(edge+1,edge+1+tot);
for(register int i=1;i<n;++i)
f[i]=i;
register int lt=n;
long long ans=0;
for(register int i=1;i<=tot&<>1;++i)
if(cmp(edge[i].u, edge[i].v))
ans+=edge[i].w,--lt;
return printf("%lld",ans),0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
luogu_bot1 |
Language |
C++ (GCC 5.4.1) |
Score |
1300 |
Code Size |
2099 Byte |
Status |
AC |
Exec Time |
48 ms |
Memory |
15360 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 |
3 ms |
7168 KB |
01-02.txt |
AC |
22 ms |
9984 KB |
01-03.txt |
AC |
43 ms |
15360 KB |
01-04.txt |
AC |
20 ms |
10496 KB |
01-05.txt |
AC |
14 ms |
10496 KB |
01-06.txt |
AC |
17 ms |
10496 KB |
01-07.txt |
AC |
16 ms |
10496 KB |
01-08.txt |
AC |
15 ms |
10496 KB |
01-09.txt |
AC |
16 ms |
10496 KB |
01-10.txt |
AC |
28 ms |
13312 KB |
01-11.txt |
AC |
34 ms |
12800 KB |
01-12.txt |
AC |
41 ms |
15360 KB |
01-13.txt |
AC |
42 ms |
15360 KB |
01-14.txt |
AC |
40 ms |
15360 KB |
01-15.txt |
AC |
42 ms |
15360 KB |
01-16.txt |
AC |
40 ms |
15360 KB |
01-17.txt |
AC |
40 ms |
15360 KB |
01-18.txt |
AC |
31 ms |
15360 KB |
01-19.txt |
AC |
15 ms |
10496 KB |
01-20.txt |
AC |
15 ms |
10496 KB |
01-21.txt |
AC |
20 ms |
12544 KB |
01-22.txt |
AC |
31 ms |
15360 KB |
01-23.txt |
AC |
32 ms |
15360 KB |
01-24.txt |
AC |
13 ms |
10496 KB |
01-25.txt |
AC |
48 ms |
15360 KB |
01-26.txt |
AC |
20 ms |
10496 KB |
01-27.txt |
AC |
18 ms |
10496 KB |
01-28.txt |
AC |
26 ms |
13312 KB |
01-29.txt |
AC |
30 ms |
12928 KB |
01-30.txt |
AC |
38 ms |
15360 KB |
sample-01.txt |
AC |
3 ms |
7168 KB |
sample-02.txt |
AC |
2 ms |
7168 KB |
sample-03.txt |
AC |
2 ms |
7168 KB |