# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1007003 | overwatch9 | Toll (BOI17_toll) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 50001;
vector <pair <int, int>> adj[maxn];
ll dis[maxn];
ll tparr[maxn][maxn];
bool processed[maxn];
queue <int> modified;
int pos[maxn];
vector <vector <ll>> pfx;
int id[maxn];
ll dijkstra(int st, int ed) {
dis[st] = 0;
priority_queue <pair <ll, int>> pq;
pq.push({0, st});
pos[st] = 0;
while (!pq.empty()) {
int s = pq.top().second;
pq.pop();
if (processed[s])
continue;
processed[s] = true;
modified.push(s);
id[s] = (int)pfx.size()-1;
pfx.back().push_back(dis[s]);
for (auto i : adj[s]) {
if (dis[i.first] > dis[s] + i.second) {
dis[i.first] = dis[s] + i.second;
pq.push({-dis[i.first], i.first});
pos[i.first] = pos[s] + 1;
}
}
}
ll ans = 0;
if (dis[ed] == 1e18) {
dis[ed] = -1;
modified.push(ed);
}
ans = dis[ed];
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int K, N, M, O;
cin >> K >> N >> M >> O;
for (int i = 0; i < M; i++) {
int a, b, t;
cin >> a >> b >> t;
adj[a].push_back({b, t});
}
fill(dis, dis + N + 1, 1e18);
pfx.emplace_back();
for (int i = 0; i < N; i++) {
if (!adj[i].empty() && !processed[i]) {
pfx.emplace_back();
dijkstra(i, i);
}
}
while (O--) {
int a, b;
cin >> a >> b;
if (id[a] != id[b] || min(id[a], id[b]) == 0)
cout << -1 << '\n';
else
cout << pfx[id[a]][pos[b]] - pfx[id[a]][pos[a]] << '\n';
}
}