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;
const long long N = 70 + 5, inf = 1e17 + 5, mod = 1e9 + 7;
int dis[N][N][N];
int edges[N][N];
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
int n, m;
cin >> n >> m;
memset(edges, '?', sizeof edges);
memset(dis, '?', sizeof dis);
for (int i = 1; i <= n; i++)
dis[i][i][0] = 0, edges[i][i] = 0;
for (int i = 0; i < m; i++)
{
int u, v, c;
cin >> u >> v >> c;
edges[u][v] = min(edges[u][v], c);
}
int k, q;
cin >> k >> q;
k = min(k, n - 1);
for (int i = 1; i <= n; i++)
{
for (int kk = 1; kk <= k; kk++)
{
for (int j = 1; j <= n; j++)
dis[i][j][kk] = dis[i][j][kk - 1];
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
if (dis[i][v][kk] > dis[i][u][kk - 1] + edges[u][v])
dis[i][v][kk] = dis[i][u][kk - 1] + edges[u][v];
}
}
while (q--)
{
int x, y;
cin >> x >> y;
if (dis[x][y][k] > (int)1e9)
cout << -1 << "\n";
else
cout << dis[x][y][k] << "\n";
}
}
/*
10
-1
0
*/
# | 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... |