# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1109112 | nh0902 | Crocodile's Underground City (IOI11_crocodile) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 110000;
long long const inf = 1e16;
int n, m, k;
pair<pair<int, int>, long long> edge[10 * N];
vector<pair<int, long long>> g[N];
vector<int> Exit;
int trace1[N], trace2[N];
long long min_dist1[N], min_dist2[N];
bool update(int x, int i, long long d) {
if (x == trace2[i]) {
if (min_dist2[i] <= d) {
return false;
}
min_dist2[i] = d;
if (min_dist1[i] > min_dist2[i]) {
swap(min_dist1[i], min_dist2[i]);
swap(trace1[i], trace2[i]);
}
return true;
}
if (x == trace1[i]) {
if (min_dist1[i] <= d) {
return false;
}
min_dist1[i] = d;
return true;
}
if (min_dist2[i] <= d) {
return false;
}
min_dist2[i] = d;
trace2[i] = x;
if (min_dist1[i] > min_dist2[i]) {
swap(min_dist1[i], min_dist2[i]);
swap(trace1[i], trace2[i]);
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
int a, b;
for (int i = 0; i < m; i ++) {
cin >> edge[i].first.first >> edge[i].first.second;
}
for (int i = 0; i < m; i ++) {
cin >> edge[i].second;
}
for (int i = 0; i < m; i ++) {
g[edge[i].first.first].push_back({edge[i].first.second, edge[i].second});
g[edge[i].first.second].push_back({edge[i].first.first, edge[i].second});
}
cin >> k;
for (int i = 0; i < k; i ++) {
cin >> a;
Exit.push_back(a);
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 0; i < n; i ++) {
min_dist1[i] = min_dist2[i] = inf;
trace1[i] = n;
trace2[i] = n;
}
for (int a : Exit) {
min_dist1[a] = min_dist2[a] = 0;
trace1[a] = a;
trace2[a] = a;
pq.push({0, a});
}
int x;
long long d;
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (d > min_dist2[x]) continue;
for (pair<int, long long> e : g[x]) {
if (update(x, e.first, d + e.second)) {
if (min_dist2[e.first] < inf) {
pq.push({min_dist2[e.first], e.first});
}
}
}
}
cout << min_dist2[0];
return 0;
}