Submission #6133601
Source Code Expand
#include <bits/stdc++.h> using namespace std; const int maxn = 200200; int u[maxn], to[maxn]; long long best[maxn]; void init(){ for(int i = 0; i < maxn; i++){ u[i] = i; to[i] = -1; best[i] = 1ll<<60; } } int find(int x){ if(x == u[x]) return x; return u[x] = find(u[x]); } long long cost[maxn]; vector<pair<int,int> > e[maxn]; int main(){ int n, q; cin >> n >> q; for(int i = 0; i < n; i++) cost[i] = 1ll<<60; for(int i = 0; i < q; i++){ int a, b; long long c; cin >> a >> b >> c; cost[a] = min(cost[a], c + 1); cost[b] = min(cost[b], c + 2); e[a].push_back({b, c}); e[b].push_back({a, c}); } for(int i = 0; i < 2 * n; i++){ int now = i % n; int prv = now - 1; if(prv < 0) prv += n; cost[now] = min(cost[now], cost[prv] + 2); } auto dist = [&](int u, int v){ if(u == v) return 0ll; long long ans = 1ll<<60; for(int rot = 0; rot < 2; rot++){ swap(u, v); if(u < v){ ans = min(ans, cost[u] + (v - u - 1) * 2); ans = min(ans, cost[v] + (n - v + u - 1) * 2); } } return ans; }; init(); set<int> all; for(int i = 0; i < n; i++){ all.insert(i); } int comps = n; long long ans = 0; while(comps > 1){ vector<pair<int,int> > vert; for(int i = 0; i < n; i++){ vert.push_back({find(i), i}); to[find(i)] = -1; best[find(i)] = 1ll<<60; } sort(vert.begin(), vert.end()); int j = 0; for(int i = 0; i < n; i = j){ while(j < n && vert[j].first == vert[i].first) j++; for(int k = i; k < j; k++) all.erase(vert[k].second); int head = vert[i].first; for(int k = i; k < j; k++){ auto it = all.lower_bound(vert[k].second); int going; if(it == all.end()) going = *all.begin(); else going = *it; long long d = dist(vert[k].second, going); int hgo = find(going); if(best[head] > d) best[head] = d, to[head] = going; if(best[hgo] > d) best[hgo] = d, to[hgo] = head; } for(int k = i; k < j; k++) all.insert(vert[k].second); } for(int i = 0; i < n; i++){ int head = find(i); for(auto ed : e[i]){ if(find(ed.first) != head){ if(best[head] > ed.second){ best[head] = ed.second; to[head] = ed.first; } if(best[find(ed.first)] > ed.second){ best[find(ed.first)] = ed.second; to[find(ed.first)] = head; } } } } vector<pair<long long, int>> heads; for(int i = 0; i < n; i++) if(i == find(i)) heads.push_back({best[i], i}); sort(heads.begin(), heads.end()); for(auto h : heads){ int head = h.second; if(find(head) != find(to[head])){ ans += best[head]; comps--; u[find(head)] = find(to[head]); } } } cout << ans << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | G - Zigzag MST |
User | FMota |
Language | C++14 (GCC 5.4.1) |
Score | 1300 |
Code Size | 2753 Byte |
Status | AC |
Exec Time | 1518 ms |
Memory | 33780 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 | 4 ms | 9600 KB |
01-02.txt | AC | 151 ms | 13028 KB |
01-03.txt | AC | 698 ms | 33268 KB |
01-04.txt | AC | 262 ms | 25844 KB |
01-05.txt | AC | 267 ms | 25716 KB |
01-06.txt | AC | 358 ms | 25460 KB |
01-07.txt | AC | 408 ms | 25716 KB |
01-08.txt | AC | 399 ms | 26352 KB |
01-09.txt | AC | 457 ms | 26480 KB |
01-10.txt | AC | 575 ms | 29552 KB |
01-11.txt | AC | 430 ms | 23416 KB |
01-12.txt | AC | 783 ms | 33652 KB |
01-13.txt | AC | 699 ms | 33780 KB |
01-14.txt | AC | 767 ms | 32756 KB |
01-15.txt | AC | 762 ms | 32884 KB |
01-16.txt | AC | 783 ms | 33652 KB |
01-17.txt | AC | 771 ms | 31988 KB |
01-18.txt | AC | 460 ms | 31468 KB |
01-19.txt | AC | 420 ms | 26228 KB |
01-20.txt | AC | 541 ms | 25072 KB |
01-21.txt | AC | 907 ms | 27764 KB |
01-22.txt | AC | 1518 ms | 33780 KB |
01-23.txt | AC | 1478 ms | 32756 KB |
01-24.txt | AC | 275 ms | 25704 KB |
01-25.txt | AC | 483 ms | 33012 KB |
01-26.txt | AC | 270 ms | 25844 KB |
01-27.txt | AC | 622 ms | 25460 KB |
01-28.txt | AC | 928 ms | 30704 KB |
01-29.txt | AC | 606 ms | 23284 KB |
01-30.txt | AC | 1176 ms | 30960 KB |
sample-01.txt | AC | 4 ms | 9600 KB |
sample-02.txt | AC | 4 ms | 9600 KB |
sample-03.txt | AC | 4 ms | 9600 KB |