Submission #6355185
Source Code Expand
#include <map>
#include <cmath>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::map; using std::string;
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;
template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
#if __cplusplus >= 201103l
template<typename T, typename... V>
void read(T &x, V&... v) { read(x), read(v...); }
#endif
const int _ = 4e5 + 10;
int n, q, ff[_], d[_];
struct Edge { int u, v, w; } e[_]; int cnt;
inline bool operator < (const Edge &a, const Edge &b) { return a.w < b.w; }
inline void addEdge(int u, int v, int w) { e[++cnt] = {u, v, w}; }
void ChkMin(int &a, int b) { if(b < a) a = b; }
int find(int x) { return ff[x] == x ? x : ff[x] = find(ff[x]); }
int main () {
read(n, q), memset(d, 127, sizeof(d[0]) * n);
for(int i = 1, x, y, z; i <= q; ++i)
read(x, y, z), addEdge(x, y, z), ChkMin(d[x], z + 1), ChkMin(d[y], z + 2);
for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i + n - 1) % n] + 2);
for(int i = 0; i < n; ++i) ChkMin(d[i], d[(i + n - 1) % n] + 2);
for(int i = 0; i < n; ++i) addEdge(i, (i + 1) % n, d[i]), ff[i] = i;
sort(e + 1, e + cnt + 1); int blo = n; ll MST = 0;
for(int i = 1; i <= cnt && blo > 1; ++i) {
int u = find(e[i].u), v = find(e[i].v);
if(u == v) continue; ff[u] = v, MST += e[i].w, --blo;
}
printf("%lld\n", MST);
return 0;
}
Submission Info
Submission Time |
|
Task |
G - Zigzag MST |
User |
water_mi |
Language |
C++14 (GCC 5.4.1) |
Score |
1300 |
Code Size |
1601 Byte |
Status |
AC |
Exec Time |
73 ms |
Memory |
9344 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 |
1 ms |
2176 KB |
01-02.txt |
AC |
38 ms |
6272 KB |
01-03.txt |
AC |
71 ms |
7296 KB |
01-04.txt |
AC |
20 ms |
9344 KB |
01-05.txt |
AC |
13 ms |
8192 KB |
01-06.txt |
AC |
17 ms |
8576 KB |
01-07.txt |
AC |
15 ms |
7424 KB |
01-08.txt |
AC |
16 ms |
7168 KB |
01-09.txt |
AC |
19 ms |
7168 KB |
01-10.txt |
AC |
44 ms |
7040 KB |
01-11.txt |
AC |
61 ms |
6656 KB |
01-12.txt |
AC |
71 ms |
7040 KB |
01-13.txt |
AC |
72 ms |
7040 KB |
01-14.txt |
AC |
71 ms |
7040 KB |
01-15.txt |
AC |
73 ms |
7040 KB |
01-16.txt |
AC |
72 ms |
7040 KB |
01-17.txt |
AC |
71 ms |
7040 KB |
01-18.txt |
AC |
53 ms |
7040 KB |
01-19.txt |
AC |
15 ms |
7424 KB |
01-20.txt |
AC |
20 ms |
7040 KB |
01-21.txt |
AC |
35 ms |
7040 KB |
01-22.txt |
AC |
60 ms |
7040 KB |
01-23.txt |
AC |
60 ms |
7040 KB |
01-24.txt |
AC |
12 ms |
7040 KB |
01-25.txt |
AC |
71 ms |
7040 KB |
01-26.txt |
AC |
18 ms |
7040 KB |
01-27.txt |
AC |
23 ms |
7040 KB |
01-28.txt |
AC |
49 ms |
7040 KB |
01-29.txt |
AC |
58 ms |
6784 KB |
01-30.txt |
AC |
70 ms |
7040 KB |
sample-01.txt |
AC |
1 ms |
2176 KB |
sample-02.txt |
AC |
1 ms |
2176 KB |
sample-03.txt |
AC |
1 ms |
2176 KB |