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;
typedef long long ll;
const ll mxN = 5e4, OO = 2e16;
ll preprocess[mxN];
bool vis[mxN];
ll COST[mxN];
vector<pair<ll, ll>> adj[mxN];
ll K, N, M, O;
void pre(ll st) {
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, st});
preprocess[st] = 0;
while(!pq.empty()) {
ll node = pq.top().second;
ll cost = pq.top().first;
pq.pop();
for (auto c : adj[node]) {
if (cost + c.first < preprocess[c.second]) {
preprocess[c.second] = cost + c.first;
pq.push({cost + c.first, c.second});
}
}
}
}
ll dijkstra(ll st, ll en) {
for (int i = 0; i < N; i++)
COST[i] = OO;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, st});
COST[st] = 0;
while(!pq.empty()) {
ll node = pq.top().second;
ll cost = pq.top().first;
if (node == en) return cost;
pq.pop();
for (auto c : adj[node]) {
if (cost + c.first < COST[c.second]) {
COST[c.second] = cost + c.first;
pq.push({cost + c.first, c.second});
}
}
}
return -1;
}
void solve() {
cin >> K >> N >> M >> O;
for (int i = 0; i < N; i++)
preprocess[i] = OO;
for (ll i = 0; i < M; i++) {
ll u, v, t;
cin >> u >> v >> t;
adj[u].push_back({t, v});
}
pre(0);
while(O--) {
ll a, b;
cin >> a >> b;
if (a == 0) {
if (preprocess[b] == OO) preprocess[b] = -1;
cout << preprocess[b] << endl;
}
else {
cout << dijkstra(a, b) << endl;
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:83:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:84:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |