Submission #2672727
Source Code Expand
/// 8:43 #include <bits/stdc++.h> using namespace std; const int inf = 2e9, Nmax = 2e5 + 5; typedef long long ll; int n, m, i, x,y, c; int d[Nmax], t[Nmax]; pair<int, pair<int,int> > edge[2*Nmax]; ll ans; int boss(int x) { if(x == t[x]) return x; return t[x] = boss(t[x]); } void unite(int x, int y) { x = boss(x), y = boss(y); t[x] = y; } int main() { // freopen("input", "r", stdin); // freopen("output", "w", stdout); cin.sync_with_stdio(false); cin >> n >> m; for(i=0; i<n; ++i) d[i] = inf, t[i] = i; for(i=1; i<=m; ++i) { cin >> x >> y >> c; d[x] = min(d[x], c+1); d[y] = min(d[y], c+2); edge[i] = {c, {x, y}}; } for(int j=0; j<2; ++j) { for(i=1; i<n; ++i) d[i] = min(d[i], d[i-1] + 2); d[0] = min(d[0], d[n-1] + 2); } for(i=0; i<n; ++i) edge[++m] = {d[i], {i, i+1}}; sort(edge+1, edge+m+1); for(i=1; i<=m; ++i) { x = edge[i].second.first; y = edge[i].second.second; c = edge[i].first; if(boss(x) != boss(y)) ans += c, unite(x, y); } cout << ans << '\n'; return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | Alexa2001 |
Language | C++14 (GCC 5.4.1) |
Score | 1300 |
Code Size | 1253 Byte |
Status | AC |
Exec Time | 106 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 | 2 ms | 2304 KB |
01-02.txt | AC | 66 ms | 6400 KB |
01-03.txt | AC | 95 ms | 6784 KB |
01-04.txt | AC | 21 ms | 8832 KB |
01-05.txt | AC | 13 ms | 7552 KB |
01-06.txt | AC | 18 ms | 8064 KB |
01-07.txt | AC | 17 ms | 6912 KB |
01-08.txt | AC | 20 ms | 6784 KB |
01-09.txt | AC | 27 ms | 6528 KB |
01-10.txt | AC | 63 ms | 6528 KB |
01-11.txt | AC | 86 ms | 6400 KB |
01-12.txt | AC | 101 ms | 6528 KB |
01-13.txt | AC | 102 ms | 6528 KB |
01-14.txt | AC | 102 ms | 6528 KB |
01-15.txt | AC | 104 ms | 6528 KB |
01-16.txt | AC | 101 ms | 6528 KB |
01-17.txt | AC | 101 ms | 6528 KB |
01-18.txt | AC | 75 ms | 9600 KB |
01-19.txt | AC | 16 ms | 7040 KB |
01-20.txt | AC | 29 ms | 6528 KB |
01-21.txt | AC | 54 ms | 6528 KB |
01-22.txt | AC | 102 ms | 6528 KB |
01-23.txt | AC | 102 ms | 6528 KB |
01-24.txt | AC | 16 ms | 9600 KB |
01-25.txt | AC | 93 ms | 9600 KB |
01-26.txt | AC | 24 ms | 9600 KB |
01-27.txt | AC | 29 ms | 8064 KB |
01-28.txt | AC | 73 ms | 8064 KB |
01-29.txt | AC | 93 ms | 7296 KB |
01-30.txt | AC | 106 ms | 8064 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 |