Submission #66657
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <iostream> #include <vector> #include <queue> #include <set> using namespace std; const long long INF = 1LL << 60; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n, m, q, k; cin >> n >> m >> q >> k; vector< int > color(n); vector<vector<pair< int , int > > > graph(n); for ( int i = 0; i < n; ++i){ cin >> color[i]; --color[i]; } for ( int i = 0; i < m; ++i){ int u, v, l; cin >> u >> v >> l; --u, --v; graph[u].emplace_back(v, l); graph[v].emplace_back(u, l); } vector< long long > dist(n, INF); priority_queue<pair< long long , int >, vector<pair< long long , int > >, greater<> > pq; dist[0] = 0; pq.emplace(0, 0); while (!pq.empty()){ auto [cur_cost, cur_node] = pq.top(); pq.pop(); if (dist[cur_node] < cur_cost) continue ; for (auto [nxt_node, l]: graph[cur_node]){ if (dist[cur_node] + l < dist[nxt_node]){ dist[nxt_node] = dist[cur_node] + l; pq.emplace(dist[nxt_node], nxt_node); } } } vector<multiset< long long > > dists(1e5); multiset< long long , greater<> > low; multiset< long long > high; auto insert = [&]( long long value){ if (low.size() < k){ low.insert(value); } else { auto itr = low.begin(); if (value < *itr){ high.insert(*itr); low.erase(itr); low.insert(value); } else { high.insert(value); } } }; auto erase = [&]( long long value){ if (high.find(value) != high.end()){ high.erase(high.find(value)); } else { low.erase(low.find(value)); if (!high.empty()){ low.insert(*high.begin()); high.erase(high.begin()); } } }; for ( int i = 0; i < n; ++i){ if (!dists[color[i]].empty()){ erase(*dists[color[i]].begin()); } dists[color[i]].insert(dist[i]); insert(*dists[color[i]].begin()); } for ( int i = 0; i < q; ++i){ int x, c; cin >> x >> c; --x, --c; erase(*dists[color[x]].begin()); dists[color[x]].erase(dists[color[x]].find(dist[x])); if (!dists[color[x]].empty()){ insert(*dists[color[x]].begin()); } color[x] = c; if (!dists[color[x]].empty()){ erase(*dists[color[x]].begin()); } dists[color[x]].insert(dist[x]); insert(*dists[color[x]].begin()); if (low.size() < k){ cout << -1 << '\n' ; } else { cout << *low.begin() << '\n' ; } } return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 1512 - Colorful Graph |
ユーザー名 | ei1903 |
投稿日時 | 2021-05-13 00:06:07 |
言語 | C++17 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 2916 Byte |
最大実行時間 | 366 ms |
最大メモリ使用量 | 32296 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01.txt | AC | 150 ms | 15704 KB |
1
|
in02.txt | AC | 70 ms | 8356 KB |
1
|
in03.txt | AC | 98 ms | 10696 KB |
1
|
in04.txt | AC | 140 ms | 12880 KB |
1
|
in05.txt | AC | 99 ms | 9568 KB |
1
|
in06.txt | AC | 366 ms | 23584 KB |
1
|
in07.txt | AC | 281 ms | 24640 KB |
1
|
in08.txt | AC | 281 ms | 25816 KB |
1
|
in09.txt | AC | 259 ms | 26236 KB |
1
|
in10.txt | AC | 241 ms | 26364 KB |
1
|
in11.txt | AC | 139 ms | 27672 KB |
1
|
in12.txt | AC | 179 ms | 27900 KB |
1
|
in13.txt | AC | 137 ms | 30676 KB |
1
|
in14.txt | AC | 135 ms | 17936 KB |
1
|
in15.txt | AC | 123 ms | 23212 KB |
1
|
in16.txt | AC | 158 ms | 32296 KB |
1
|
in17.txt | AC | 276 ms | 31572 KB |
1
|
in18.txt | AC | 199 ms | 31412 KB |
1
|
in19.txt | AC | 260 ms | 31780 KB |
1
|
in20.txt | AC | 74 ms | 18340 KB |
1
|
in21.txt | AC | 76 ms | 20124 KB |
1
|
in22.txt | AC | 91 ms | 21156 KB |
1
|
in23.txt | AC | 72 ms | 20768 KB |
1
|
in24.txt | AC | 71 ms | 21284 KB |
1
|
in25.txt | AC | 77 ms | 23356 KB |
1
|
in26.txt | AC | 75 ms | 24172 KB |
1
|
in27.txt | AC | 53 ms | 22844 KB |
1
|
in28.txt | AC | 47 ms | 23096 KB |
1
|
sample01.txt | AC | 22 ms | 22960 KB |
1
|
sample02.txt | AC | 21 ms | 23092 KB |
1
|