Walking Plan
Time Limit: 5000/2500 MS (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 T(1≤T≤10), denoting the number of test cases.
In each test case, there are 2 integers n,m(2≤n≤50,1≤m≤10000) in the first line, denoting the number of intersections and one way streets.
In the next m lines, each line contains 3 integers ui,vi,wi(1≤ui,vi≤n,ui≠vi,1≤wi≤10000), denoting a one way street from the intersection ui to vi, and the length of it is wi.
Then in the next line, there is an integer q(1≤q≤100000), denoting the number of days.
In the next q lines, each line contains 3 integers si,ti,ki(1≤si,ti≤n,1≤ki≤10000), 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
题意:有向图,q次询问,s到t至少经过k条边的最短路径.
思路:因为k最大是10000,所以我们如果用Floyd预处理出来任意两点的至少经过k条边的最短路径,得50*50*50*10000=1e9.
时限不允许,故我们可以尝试分块,把10000分成100*100,用dp1[l][i][j]表示从i到j恰好经过l条边的最短路径,用Floyd把l跑到150左右,然后倒叙维护,让dp1[l][i][j]表示从i到j至少经过l条边的最短路径,所以前面我们到150. 然后dp2[l][i][j]表示从i到j至少经过l*100条边的最短路径,继续用Floyd把l跑到100. 每次询问如果k<= 100,则直接打印dp1,否则用dp2和dp1一块打印.
代码:
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e5+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;
int n,m;
ll dp1[234][52][52],dp2[234][52][52];
void init()
{
for(int i = 1;i<= 50;i++)
for(int j = 1;j<= 50;j++)
for(int l = 1;l<= 200;l++)
dp1[l][i][j] = dp2[l][i][j] = inf;
}
int main()
{
int t;
cin>>t;
while(t--)
{
init();
scanf("%d %d",&n,&m);
for(int i = 1;i<= m;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(c< dp1[1][a][b])
dp1[1][a][b] = c;
}
for(int l = 2;l<= 200;l++)//恰好经过l条边
for(int k = 1;k<= n;k++)
for(int i = 1;i<= n;i++)
for(int j = 1;j<= n;j++)
dp1[l][i][j] = min(dp1[l][i][j],dp1[l-1][i][k]+dp1[1][k][j]);
for(int i = 1;i<= n;i++)
for(int j = 1;j<= n;j++)
for(int l = 199;l>= 1;l--)//至少经过l条边
dp1[l][i][j] = min(dp1[l][i][j],dp1[l+1][i][j]);
for(int i = 1;i<= n;i++)
for(int j = 1;j<= n;j++)
dp2[1][i][j] = dp1[100][i][j];
for(int l = 2;l<= 100;l++)//至少经过l*100条边
for(int k = 1;k<= n;k++)
for(int i = 1;i<= n;i++)
for(int j = 1;j<= n;j++)
dp2[l][i][j] = min(dp2[l][i][j],dp2[l-1][i][k]+dp2[1][k][j]);
int q;
cin>>q;
while(q--)
{
int s,t,k;
scanf("%d %d %d",&s,&t,&k);
int p = (k-1)/100,q = k-100*p;
ll ans = inf;
if(k<= 100)
ans = dp1[k][s][t];
else
{
for(int i = 1;i<= n;i++)
ans = min(ans,dp2[p][s][i]+dp1[q][i][t]);
}
if(ans>= inf) printf("-1\n");
else printf("%lld\n",ans);
}
}
return 0;
}