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>
#define int long long
#define F first
#define S second
#define T int tc; cin >> tc; while(tc--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int inf = 1e18;
const int N = 1e6 + 5;
const int M = 505;
const int LOG = 32;
const int CHAR = 26;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
const float pi = atan(1) * 4;
const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {0, -1, 1, -1, 1, 0, -1, 1};
const char d[] = {'E', 'N', 'S', 'W'};
int n, m, k, q;
vector<pair<int, int>> adj[100];
priority_queue<pair<pair<int, int>, int>> pq;
int g[100][100], dist[100][100][100], ans[100][100];
void dij(int node) {
for(int i = 1; i <= n; i++)
for(int j = 0; j <= n; j++)
dist[node][i][j] = inf;
dist[node][node][0] = 0;
pq.push({{0, 0}, node});
while(!pq.empty()) {
int u = pq.top().S;
int w = -pq.top().F.F;
int y = -pq.top().F.S;
pq.pop();
for(auto x: adj[u]) {
if (dist[node][x.F][y + 1] > dist[node][u][y] + x.S) {
dist[node][x.F][y + 1] = dist[node][u][y] + x.S;
pq.push({{-dist[node][x.F][y + 1], -y - 1}, x.F});
}
}
}
}
main() {
IOS
cin >> n >> m;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) g[i][j] = inf;
for(int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u][v] = min(g[u][v], w);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if (g[i][j] != inf) adj[i].push_back({j, g[i][j]});
cin >> k >> q;
for(int i = 1; i <= n; i++) {
dij(i);
for(int j = 1; j <= n; j++) {
ans[i][j] = inf;
for(int y = 0; y <= k; y++)
ans[i][j] = min(ans[i][j], dist[i][j][y]);
if (ans[i][j] == inf) ans[i][j] = -1;
}
}
while(q--) {
int u, v;
cin >> u >> v;
cout << ans[u][v] << '\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'void dij(long long int)':
Main.cpp:32:13: warning: unused variable 'w' [-Wunused-variable]
32 | int w = -pq.top().F.F;
| ^
Main.cpp: At global scope:
Main.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main() {
| ^~~~
# | 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... |