# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
971427 | Nomio | Cyberland (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k, h;
cin >> n >> m >> k >> h;
vector<pair<int, int>> v[n];
while(m--) {
int x, y, c;
cin >> x >> y >> c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
vector<bool> V(n, 0);
for(int i = 0; i < n; i++) {
int a;
cin >> a;
if(a == 0) {
V[i] = 1;
}
}
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
vector<ll> dis(n, 1e18);
dis[0] = 0;
pq.push({0, 0});
while(!pq.empty()) {
ll x = pq.top().first;
int y = pq.top().second;
pq.pop();
if(dis[y] != x) continue;
for(pair<int, int> p : v[y]) {
int X = p.first;
int Y = p.second;
if(V[X] && dis[X] != 0) {
dis[X] = 0;
pq.push({0, X});
} else if(dis[X] > dis[y] + Y) {
dis[X] = dis[y] + Y;
pq.push({dis[X], X});
}
}
}
if(dis[h] == 1e18) {
cout << -1 << '\n';
} else {
cout << dis[h] << '\n';
}
return 0;
}