Submission #4412869
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct edge
{
int u , v;
ll w;
}E[400010];
int len , head[200010];
void add(int u , int v , ll w)
{
E[len++] = {u , v , w};
}
bool cmp(edge a , edge b)
{
return a.w < b.w;
}
int n , a , b , q;
int fa[200010];
ll d[200010];
int get(int x)
{
return (fa[x] == x) ? x : (fa[x] = get(fa[x]));
}
ll kruskal()
{
ll res = 0;
int cnt = n;
sort(E , E + len , cmp);
for (int i = 0; i < len; i++)
{
int u = E[i].u , v = E[i].v;
ll w = E[i].w;
//cout << u << " " << v << " " << w << endl;
u = get(u) , v = get(v);
if (u != v)
{
fa[v] = u;
res += w;
cnt--;
}
if (cnt == 1)
{
return res;
}
}
return res;
}
int main()
{
memset(d , 0x7f , sizeof(d));
ll c;
scanf("%d%d" , &n , &q);
for (int i = 0; i < n; i++)
{
fa[i] = i;
}
for (int i = 1; i <= q; i++)
{
scanf("%d%d%lld" , &a , &b , &c);
add(a , b , c);
d[a] = min(d[a] , c + 1);
d[b] = min(d[b] , c + 2);
//cout << a << " " << b <<" " << d[a] << " " << d[b] << endl;
}
for (int i = 0; i < n; i++)
{
d[(i + 1) % n] = min(d[(i + 1) % n] , d[i] + 2);
}
for (int i = 0; i < n; i++)
{
d[(i + 1) % n] = min(d[(i + 1) % n] , d[i] + 2);
}
for (int i = 0; i < n; i++)
{
add(i , (i + 1) % n , d[i]);
}
printf("%lld\n" , kruskal());
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
luogu_bot3 |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
1390 Byte |
Status |
AC |
Exec Time |
119 ms |
Memory |
9600 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:54:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d" , &n , &q);
^
./Main.cpp:61:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld" , &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 |
3840 KB |
01-02.txt |
AC |
70 ms |
7936 KB |
01-03.txt |
AC |
101 ms |
9600 KB |
01-04.txt |
AC |
31 ms |
8448 KB |
01-05.txt |
AC |
21 ms |
8448 KB |
01-06.txt |
AC |
25 ms |
8448 KB |
01-07.txt |
AC |
21 ms |
8448 KB |
01-08.txt |
AC |
20 ms |
8448 KB |
01-09.txt |
AC |
23 ms |
8448 KB |
01-10.txt |
AC |
57 ms |
8448 KB |
01-11.txt |
AC |
88 ms |
8320 KB |
01-12.txt |
AC |
97 ms |
9600 KB |
01-13.txt |
AC |
98 ms |
9600 KB |
01-14.txt |
AC |
97 ms |
9600 KB |
01-15.txt |
AC |
100 ms |
9600 KB |
01-16.txt |
AC |
98 ms |
9600 KB |
01-17.txt |
AC |
97 ms |
9600 KB |
01-18.txt |
AC |
84 ms |
9600 KB |
01-19.txt |
AC |
21 ms |
8448 KB |
01-20.txt |
AC |
18 ms |
8448 KB |
01-21.txt |
AC |
38 ms |
8448 KB |
01-22.txt |
AC |
87 ms |
9600 KB |
01-23.txt |
AC |
87 ms |
9600 KB |
01-24.txt |
AC |
22 ms |
8448 KB |
01-25.txt |
AC |
119 ms |
9600 KB |
01-26.txt |
AC |
31 ms |
8448 KB |
01-27.txt |
AC |
27 ms |
8448 KB |
01-28.txt |
AC |
67 ms |
8448 KB |
01-29.txt |
AC |
88 ms |
8448 KB |
01-30.txt |
AC |
100 ms |
9600 KB |
sample-01.txt |
AC |
2 ms |
3840 KB |
sample-02.txt |
AC |
2 ms |
3840 KB |
sample-03.txt |
AC |
2 ms |
3840 KB |