Skip to content

Commit 64b9d01

Browse files
authored
[20250930] BOJ / P3 / Alternative Mart / 권혁준
1 parent 8653e87 commit 64b9d01

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N, M, K, Q;
6+
vector<vector<pair<int, int>>> V(50001);
7+
vector<deque<pair<int, int>>> D(50001);
8+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> PQ;
9+
10+
int main() {
11+
cin.tie(0)->sync_with_stdio(0);
12+
13+
for (cin >> N >> M >> K >> Q; K--;) {
14+
int a;
15+
cin >> a;
16+
PQ.emplace(0, a, a);
17+
D[a].emplace_back(0, a);
18+
}
19+
for (int a, b, c; M--; V[a].emplace_back(b, c), V[b].emplace_back(a, c)) cin >> a >> b >> c;
20+
21+
while (!PQ.empty()) {
22+
auto [d, p, n] = PQ.top(); PQ.pop();
23+
bool con = false, ex = false;
24+
for (auto [_, q] : D[n]) {
25+
if (q == p) {
26+
ex = 1;
27+
if (d > _) con = 1;
28+
}
29+
}
30+
if (!ex) con = 1;
31+
if (con) continue;
32+
for (auto [i, c] : V[n]) {
33+
int org = -1;
34+
for (auto [_, q] : D[i]) {
35+
if (p == q) org = _;
36+
}
37+
stack<pair<int, int>> tmp;
38+
if (org == -1) {
39+
while (!D[i].empty() && D[i].back().first > d+c) {
40+
tmp.push(D[i].back());
41+
D[i].pop_back();
42+
}
43+
if (D[i].size() < 11) {
44+
D[i].emplace_back(d + c, p);
45+
PQ.emplace(d + c, p, i);
46+
}
47+
while (D[i].size() < 11 && !tmp.empty()) {
48+
D[i].emplace_back(tmp.top());
49+
tmp.pop();
50+
}
51+
}
52+
else if (d + c < org) {
53+
while (!D[i].empty() && D[i].back().first > d+c) {
54+
if(D[i].back().second != p) tmp.push(D[i].back());
55+
D[i].pop_back();
56+
}
57+
D[i].emplace_back(d + c, p);
58+
while (!tmp.empty()) {
59+
D[i].emplace_back(tmp.top());
60+
tmp.pop();
61+
}
62+
PQ.emplace(d + c, p, i);
63+
}
64+
}
65+
}
66+
67+
for (int s, c, a; Q--;) {
68+
vector<int> t;
69+
for (cin >> s >> c; c--; t.push_back(a)) cin >> a;
70+
bool pr = false;
71+
for (int i = 0; i < D[s].size(); i++) {
72+
bool cant = false;
73+
for (int j : t) cant |= j == D[s][i].second;
74+
if (!cant) {
75+
pr = true;
76+
cout << D[s][i].second << ' ' << D[s][i].first << '\n';
77+
break;
78+
}
79+
}
80+
}
81+
82+
}
83+
```

0 commit comments

Comments
 (0)