Submission #66716


ソースコード

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
#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 < 1e5; ++i){
dists[i].insert(INF);
}
for(int i = 0; i < n; ++i){
dists[color[i]].insert(dist[i]);
}
for(int i = 0; i < 1e5; ++i){
insert(*dists[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]));
insert(*dists[color[x]].begin());
color[x] = c;
erase(*dists[color[x]].begin());
dists[color[x]].insert(dist[x]);
insert(*dists[color[x]].begin());
long long ans = *low.begin();
if(ans == INF){
cout << -1 << '\n';
}else{
cout << ans << '\n';
}
}
return (0);
}

ステータス

項目 データ
問題 1512 - Colorful Graph
ユーザー名 ei1903
投稿日時 2021-05-16 15:38:51
言語 C++17
状態 Accepted
得点 5
ソースコード長 2865 Byte
最大実行時間 336 ms
最大メモリ使用量 38512 KB

セット

セット 得点 Cases
1 ALL 5 / 5 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt AC 193 ms 22996 KB
1
in02.txt AC 111 ms 17344 KB
1
in03.txt AC 157 ms 19208 KB
1
in04.txt AC 229 ms 21264 KB
1
in05.txt AC 135 ms 18808 KB
1
in06.txt AC 301 ms 30004 KB
1
in07.txt AC 305 ms 31188 KB
1
in08.txt AC 302 ms 32364 KB
1
in09.txt AC 336 ms 32652 KB
1
in10.txt AC 336 ms 32904 KB
1
in11.txt AC 142 ms 32412 KB
1
in12.txt AC 158 ms 32564 KB
1
in13.txt AC 173 ms 35264 KB
1
in14.txt AC 195 ms 26280 KB
1
in15.txt AC 192 ms 30276 KB
1
in16.txt AC 217 ms 36932 KB
1
in17.txt AC 317 ms 38040 KB
1
in18.txt AC 256 ms 38512 KB
1
in19.txt AC 298 ms 38356 KB
1
in20.txt AC 124 ms 27596 KB
1
in21.txt AC 141 ms 29324 KB
1
in22.txt AC 145 ms 30364 KB
1
in23.txt AC 122 ms 30040 KB
1
in24.txt AC 118 ms 30596 KB
1
in25.txt AC 137 ms 32652 KB
1
in26.txt AC 128 ms 33456 KB
1
in27.txt AC 117 ms 32340 KB
1
in28.txt AC 101 ms 32560 KB
1
sample01.txt AC 37 ms 32524 KB
1
sample02.txt AC 48 ms 32496 KB
1