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 <iostream>
using namespace std;
const int inf = 1000000000;
int n, m;
int adj[70][70];
int k, q;
int dp[70][70][70]; //[distance][town A][town B]
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
adj[i][j] = inf;
for(int d = 0; d < n; d++)
dp[d][i][j] = inf;
}
for(int i = 0; i < n; i++)
{
adj[i][i] = 0;
dp[0][i][i] = 0;
}
for(int i = 0; i < m; i++)
{
int a, b, t;
cin>>a>>b>>t;
a--;
b--;
adj[a][b] = min(adj[a][b], t);
}
cin>>k>>q;
if(k > n - 1)
k = n - 1;
for(int d = 1; d <= k; d++)
for(int a = 0; a < n; a++)
for(int b = 0; b < n; b++)
for(int x = 0; x < n; x++)
if(dp[d - 1][a][x] != inf && adj[x][b] != inf)
dp[d][a][b] = min(dp[d][a][b], dp[d - 1][a][x] + adj[x][b]);
while(q--)
{
int from, to;
cin>>from>>to;
from--;
to--;
if(dp[k][from][to] == inf)
cout<<-1<<endl;
else
cout<<dp[k][from][to]<<endl;
}
return 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... |