Problem M. Walking Plan
Time Limit: 5000/2500 MS (Java/Others)
Memory Limit: 524288/524288 K (Java/Others)
Problem Description
There are n intersections in Bytetown, connected with m one way streets. Little Q likes sport walking very much, he plans to walk for q days. On the i-th day, Little Q plans to start walking at the si-th intersection, walk through at least ki streets and finally return to the ti-th intersection.
Little Q’s smart phone will record his walking route. Compared to stay healthy, Little Q cares the statistics more. So he wants to minimize the total walking length of each day. Please write a program to help him find the best route.
Input
The first line of the input contains an integer
, denoting the number of test cases.In each test case, there are 2 integers
in the first line, denoting the number of intersections and one way streets.In the next m lines, each line contains 3 integers
, denoting a one way street from the intersection
to
, and the length of it is
.
Then in the next line, there is an integer
, denoting the number of days.In the next q lines, each line contains 3 integers
, describing the walking plan.
Output
For each walking plan, print a single line containing an integer, denoting the minimum total walking length. If there is no solution, please print -1.
Sample Input
2
3 3
1 2 1
2 3 10
3 1 100
3
1 1 1
1 2 1
1 3 1
2 1
1 2 1
1
2 1 1
Sample Output
111
1
11
-1
思路:DP。
表示从
到
恰好经过1条路径的最短长度。
表示从
到
恰好经过
条路径的最短长度。
表示从
到
恰好经过
条路径的最短长度。
虽然上面2个数组满足了询问给出的至少
条路径的条件
但可能走k条以上的路径会更优。
所以最后要用Floyd来更新B数组得到至少经过
条路径的最短路.
%
#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
const int INF=1e9+7;
typedef long long ll;
int g[55][55];
int d[55][55];
int f[55][55];
int A[101][55][55];
int B[101][55][55];
int n;
void update(int a[][55],int b[][55],int c[][55])
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)f[i][j]=INF;
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)f[i][j]=min(f[i][j],a[i][k]+b[k][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)c[i][j]=f[i][j];
}
int main()
{
int T;
cin>>T;
while(T--)
{
int m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)g[i][j]=INF;
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]=min(g[x][y],z);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)A[0][i][j]=B[0][i][j]=(i==j?0:INF);
for(int i=1;i<=100;i++)update(A[i-1],g,A[i]);//更新A数组
for(int i=1;i<=100;i++)update(B[i-1],A[100],B[i]);//用A[100]去更新B数组
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)g[i][j]=i==j?0:g[i][j];
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
for(int t=0;t<=100;t++)//用Floyd去更新B数组
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)f[i][j]=INF;
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)f[i][j]=min(f[i][j],B[t][i][k]+g[k][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)B[t][i][j]=min(B[t][i][j],f[i][j]);
}
scanf("%d",&m);
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
int ans=INF;
for(int i=1;i<=n;i++)ans=min(ans,B[z/100][x][i]+A[z%100][i][y]);
printf("%d\n",ans==INF?-1:ans);
}
}
return 0;
}