Submission #6359766
Source Code Expand
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <vector> #define itn int #define reaD read #define N 200001 using namespace std; int n, Q, f[N], cnt, fa[N]; struct edge { int from, to, cost; bool operator < (const edge &p) const { return cost < p.cost; } } e[N << 2]; inline int read() { int x = 0, w = 1; char c = getchar(); while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * w; } inline void adde(int u, int v, int w) { e[++cnt] = (edge) { u, v, w }; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } long long Kruscal() { sort(e + 1, e + cnt + 1); long long ans = 0; for(int i = 1; i <= cnt; i++) { int u = find(e[i].from), v = find(e[i].to), w = e[i].cost; if(u == v) continue; fa[u] = v; ans += w; } return ans; } int main() { n = read(); Q = read(); for(int i = 1; i <= n; i++) fa[i] = i; memset(f, 0x3f, sizeof(f)); while(Q--) { int u = read() + 1, v = read() + 1, w = reaD(); adde(u, v, w); f[u] = min(f[u], w + 1); f[v] = min(f[v], w + 2); } int pos = 1; for(int i = 2; i <= n; i++) if(f[i] < f[pos]) pos = i; for(int i = pos; i <= pos + n - 1; i++) f[i % n + 1] = min(f[i % n + 1], f[(i - 1) % n + 1] + 2); for(int i = 1; i <= n; i++) adde(i, i % n + 1, f[i]); printf("%lld\n", Kruscal()); return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | vjudge5 |
Language | C++14 (GCC 5.4.1) |
Score | 1300 |
Code Size | 1438 Byte |
Status | AC |
Exec Time | 75 ms |
Memory | 12288 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 | 2 ms | 2944 KB |
01-02.txt | AC | 42 ms | 7040 KB |
01-03.txt | AC | 72 ms | 9344 KB |
01-04.txt | AC | 21 ms | 9344 KB |
01-05.txt | AC | 14 ms | 8192 KB |
01-06.txt | AC | 18 ms | 8576 KB |
01-07.txt | AC | 16 ms | 7424 KB |
01-08.txt | AC | 17 ms | 7168 KB |
01-09.txt | AC | 21 ms | 7168 KB |
01-10.txt | AC | 46 ms | 7040 KB |
01-11.txt | AC | 61 ms | 7040 KB |
01-12.txt | AC | 73 ms | 9088 KB |
01-13.txt | AC | 73 ms | 9088 KB |
01-14.txt | AC | 73 ms | 9088 KB |
01-15.txt | AC | 75 ms | 9088 KB |
01-16.txt | AC | 75 ms | 9088 KB |
01-17.txt | AC | 73 ms | 9216 KB |
01-18.txt | AC | 53 ms | 12288 KB |
01-19.txt | AC | 16 ms | 7424 KB |
01-20.txt | AC | 19 ms | 7040 KB |
01-21.txt | AC | 32 ms | 7040 KB |
01-22.txt | AC | 56 ms | 9088 KB |
01-23.txt | AC | 56 ms | 9088 KB |
01-24.txt | AC | 16 ms | 10240 KB |
01-25.txt | AC | 73 ms | 12288 KB |
01-26.txt | AC | 21 ms | 9728 KB |
01-27.txt | AC | 23 ms | 8704 KB |
01-28.txt | AC | 48 ms | 8704 KB |
01-29.txt | AC | 57 ms | 7936 KB |
01-30.txt | AC | 68 ms | 10752 KB |
sample-01.txt | AC | 2 ms | 2944 KB |
sample-02.txt | AC | 2 ms | 2944 KB |
sample-03.txt | AC | 2 ms | 2944 KB |