Submission #6637717
Source Code Expand
// luogu-judger-enable-o2
/*
Problem : luogu AT2134
Algorithm : MST
Status :
*/
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
int n,m,tot,cnt;
ll ans;
int d[MAXN],f[MAXN];
struct Edge{
int u,v,w;
} e[MAXN * 2];
bool cmp(const Edge &x,const Edge &y){
return x.w < y.w;
}
void add(int u,int v,int w){
e[++tot].u = u;
e[tot].v = v;
e[tot].w = w;
}
int find(int x){
if(x == f[x])
return x;
else
return f[x] = find(f[x]);
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 0;i <= n;i++){
f[i] = i;
d[i] = INF;
}
for(int i = 1;i <= m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
d[a] = min(d[a],c + 1);
d[b] = min(d[b],c + 2);
}
for(int i = 0;i < n;i++)
d[i] = min(d[i],d[(i - 1 + n) % n] + 2);
for(int i = 0;i < n;i++)
d[i] = min(d[i],d[(i - 1 + n) % n] + 2);
for(int i = 0;i < n;i++)
add(i,(i + 1) % n,d[i]);
sort(e + 1,e + 1 + tot,cmp);
for(int i = 1;i <= tot;i++){
int x = e[i].u;
int y = e[i].v;
if(find(x) != find(y)){
f[find(x)] = find(y);
ans += e[i].w;
cnt++;
if(cnt == n - 1)
break;
}
}
printf("%lld\n",ans);
return 0;
}
Submission Info
Submission Time
2019-08-01 23:34:56+0900
Task
G - Zigzag MST
User
luogu_bot5
Language
C++ (GCC 5.4.1)
Score
1300
Code Size
1440 Byte
Status
AC
Exec Time
115 ms
Memory
8832 KB
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:47:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
./Main.cpp:54:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&a,&b,&c);
^
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
2304 KB
01-02.txt
AC
66 ms
6400 KB
01-03.txt
AC
99 ms
6784 KB
01-04.txt
AC
33 ms
8832 KB
01-05.txt
AC
22 ms
7552 KB
01-06.txt
AC
28 ms
8064 KB
01-07.txt
AC
23 ms
6912 KB
01-08.txt
AC
23 ms
6656 KB
01-09.txt
AC
26 ms
6528 KB
01-10.txt
AC
59 ms
6528 KB
01-11.txt
AC
85 ms
6400 KB
01-12.txt
AC
97 ms
6528 KB
01-13.txt
AC
97 ms
6528 KB
01-14.txt
AC
97 ms
6528 KB
01-15.txt
AC
99 ms
6528 KB
01-16.txt
AC
97 ms
6528 KB
01-17.txt
AC
97 ms
6528 KB
01-18.txt
AC
80 ms
6528 KB
01-19.txt
AC
23 ms
6784 KB
01-20.txt
AC
24 ms
6528 KB
01-21.txt
AC
44 ms
6528 KB
01-22.txt
AC
86 ms
6528 KB
01-23.txt
AC
86 ms
6528 KB
01-24.txt
AC
22 ms
6528 KB
01-25.txt
AC
115 ms
6528 KB
01-26.txt
AC
32 ms
6528 KB
01-27.txt
AC
29 ms
6528 KB
01-28.txt
AC
67 ms
6528 KB
01-29.txt
AC
83 ms
6400 KB
01-30.txt
AC
96 ms
6528 KB
sample-01.txt
AC
2 ms
2304 KB
sample-02.txt
AC
2 ms
2304 KB
sample-03.txt
AC
2 ms
2304 KB