# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1007003 | overwatch9 | Toll (BOI17_toll) | 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;
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';
}
}