This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Bismi ALlah
#include "bits/stdc++.h"
using namespace std;
signed main () {
int n, m, k, query;
cin >> k >> n >> m >> query;
vector<pair <int,int>> g[n];
vector <int> used(n);
function <void(int)> dfs=[&](int v) {
used[v] = 1;
for(auto [u, w] : g[v]) {
if(!used[u]) dfs(u);
else if(used[u] == 1) {
assert(false);
}
}
used[v] = 2;
};
for(int i = 0; i < m; i ++) {
int u, v;
cin >> u >> v;
int w;
cin >> w;
g[u].push_back({v, w});
}
for(int i = 0; i < n; i ++) {
if(used[i] == 0) dfs(i);
}
priority_queue <pair <int,int>> q;
vector <int> dist(n, 1e9);
dist[0] = 0;
q.push({0, 0});
while(!q.empty()) {
int v = q.top().second, cur_d = q.top().first;
q.pop();
if(cur_d > dist[v]) continue;
for(auto [u, w] : g[v]) {
if(dist[u] > dist[v] + w) {
dist[u] = dist[v] + w;
q.push({-dist[u], u});
}
}
}
while(query --) {
int a, b;
cin >> a >> b;
if(a == 0) {
if(dist[b] == 1e9) cout << -1 << "\n";
else
cout << dist[b] << "\n";
}else {
vector <int> dist2(n, 1e9);
dist2[a] = 0;
q.push({0, a});
while(!q.empty()) {
int v = q.top().second, cur_d = q.top().first;
q.pop();
if(cur_d > dist2[v]) continue;
for(auto [u, w] : g[v]) {
if(dist2[u] > dist2[v] + w) {
dist2[u] = dist2[v] + w;
q.push({-dist2[u], u});
}
}
}
if(dist2[b] == 1e9) cout << -1;
else cout << dist2[b];
cout << "\n";
}
}
return 0;
}
Compilation message (stderr)
toll.cpp: In lambda function:
toll.cpp:13:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
13 | for(auto [u, w] : g[v]) {
| ^
toll.cpp: In function 'int main()':
toll.cpp:39:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for(auto [u, w] : g[v]) {
| ^
toll.cpp:62:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for(auto [u, w] : g[v]) {
| ^
# | 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... |