Submission #4420252
Source Code Expand
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; inline int gi() { register int data = 0, w = 1; register char ch = 0; while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); return w * data; } const int MAX_N = 2e5 + 5; const int INF = 2e9; struct Edge { int u, v, w; } e[MAX_N << 1]; bool operator < (const Edge &l, const Edge &r) { return l.w < r.w; } int N, cnt, Q, ds[MAX_N]; int pa[MAX_N]; int getf(int x) { return x == pa[x] ? x : pa[x] = getf(pa[x]); } long long ans = 0; int main () { N = gi(), Q = gi(); for (int i = 1; i <= N; i++) ds[i] = INF; while (Q--) { int A = gi() + 1, B = gi() + 1, C = gi(); e[++cnt] = (Edge){A, B, C}; ds[A % N + 1] = min(ds[A % N + 1], C + 1); ds[B % N + 1] = min(ds[B % N + 1], C + 2); } int pos = min_element(&ds[1], &ds[N + 1]) - ds; for (int i = pos % N + 1, j = pos; i != pos; j = i, i = i % N + 1) ds[i] = min(ds[j] + 2, ds[i]); for (int i = 2; i <= N; i++) e[++cnt] = (Edge){i - 1, i, ds[i]}; e[++cnt] = (Edge){N, 1, ds[1]}; sort(&e[1], &e[cnt + 1]); for (int i = 1; i <= N; i++) pa[i] = i; for (int i = 1; i <= cnt; i++) { int u = getf(e[i].u), v = getf(e[i].v); if (u == v) continue; ans += e[i].w, pa[u] = v; } printf("%lld\n", ans); return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | luogu_bot2 |
Language | C++ (GCC 5.4.1) |
Score | 1300 |
Code Size | 1463 Byte |
Status | AC |
Exec Time | 82 ms |
Memory | 9600 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 | 256 KB |
01-02.txt | AC | 41 ms | 2816 KB |
01-03.txt | AC | 78 ms | 6784 KB |
01-04.txt | AC | 21 ms | 6656 KB |
01-05.txt | AC | 14 ms | 5376 KB |
01-06.txt | AC | 18 ms | 5888 KB |
01-07.txt | AC | 16 ms | 4736 KB |
01-08.txt | AC | 16 ms | 4480 KB |
01-09.txt | AC | 21 ms | 4352 KB |
01-10.txt | AC | 48 ms | 6400 KB |
01-11.txt | AC | 64 ms | 5632 KB |
01-12.txt | AC | 82 ms | 6528 KB |
01-13.txt | AC | 76 ms | 6528 KB |
01-14.txt | AC | 76 ms | 6528 KB |
01-15.txt | AC | 78 ms | 6528 KB |
01-16.txt | AC | 76 ms | 6528 KB |
01-17.txt | AC | 76 ms | 6528 KB |
01-18.txt | AC | 55 ms | 9600 KB |
01-19.txt | AC | 15 ms | 4608 KB |
01-20.txt | AC | 20 ms | 4352 KB |
01-21.txt | AC | 34 ms | 6400 KB |
01-22.txt | AC | 59 ms | 6528 KB |
01-23.txt | AC | 62 ms | 6528 KB |
01-24.txt | AC | 16 ms | 7424 KB |
01-25.txt | AC | 75 ms | 9600 KB |
01-26.txt | AC | 21 ms | 6912 KB |
01-27.txt | AC | 23 ms | 5888 KB |
01-28.txt | AC | 50 ms | 7936 KB |
01-29.txt | AC | 62 ms | 6528 KB |
01-30.txt | AC | 71 ms | 8064 KB |
sample-01.txt | AC | 1 ms | 256 KB |
sample-02.txt | AC | 1 ms | 256 KB |
sample-03.txt | AC | 1 ms | 256 KB |