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 qq days. On the i-th day, Little Q plans to start walking at the intersection, walk through at least kiki streets and finally return to the ti−thti−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)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)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)ui,vi,wi(1≤ui,vi≤n,ui≠vi,1≤wi≤10000), denoting a one way street from the intersection uiui to vivi, and the length of it is wiwi.
Then in the next line, there is an integer q(1≤q≤100000)q(1≤q≤100000), denoting the number of days.
In the next qq 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−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
Source
2018 Multi-University Training Contest 3
题意:
一张有向有边权图,q组询问,每组询问a,b,k,询问从a到b至少经过k条边的最短路。
题解:
考虑分块,
f[k][i][j]f[k][i][j] 表示刚好走k(k<=100)k(k<=100)步从ii走到的最短路程
g[k][i][j]g[k][i][j]表示刚好走100∗k(k<=100)100∗k(k<=100)步从ii走到的最短路程
ro[k][i][j]ro[k][i][j]表示至少走k(k<=100)k(k<=100)步从ii走到的最短路程
val[i][j]val[i][j]表示从ii到直接连边的权值
mp[i][j]mp[i][j]表示从ii到没有限制步数的最短路距离
可得:
f[k][i][j]=min(f[k][i][j],f[k−1][i][mid]+val[mid][j])f[k][i][j]=min(f[k][i][j],f[k−1][i][mid]+val[mid][j])
g[k][i][j]=min(g[k][i][j],g[k−1][i][mid]+f[100][mid][j])g[k][i][j]=min(g[k][i][j],g[k−1][i][mid]+f[100][mid][j])
ro[k][i][j]=min(ro[k][i][j],f[k][i][mid]+mp[mid][j])ro[k][i][j]=min(ro[k][i][j],f[k][i][mid]+mp[mid][j])
答案为min(g[k/100][a][i]+ro[kmin(g[k/100][a][i]+ro[k MODMOD 100][i][b])100][i][b]) i∈[1,n]i∈[1,n]
#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
#define INF 1050000000
using namespace std;
int val[54][54];
int f[104][54][54],g[104][54][54],ro[104][54][54],mp[54][54];
int n,m;
int pre(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
mp[i][j]=val[i][j];
}
}
for(int i=1;i<=n;i++)mp[i][i]=0;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
for(int k=0;k<=100;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
f[k][i][j]=INF;
}
}
}
for(int i=1;i<=n;i++)f[0][i][i]=0;
for(int k=1;k<=100;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int mid=1;mid<=n;mid++){
f[k][i][j]=min(f[k][i][j],f[k-1][i][mid]+val[mid][j]);
}
}
}
}
for(int k=0;k<=100;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
g[k][i][j]=INF;
}
}
}
for(int i=1;i<=n;i++)g[0][i][i]=0;
for(int k=1;k<=100;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int mid=1;mid<=n;mid++){
g[k][i][j]=min(g[k][i][j],g[k-1][i][mid]+f[100][mid][j]);
}
}
}
}
for(int k=0;k<=100;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ro[k][i][j]=INF;
for(int mid=1;mid<=n;mid++){
ro[k][i][j]=min(ro[k][i][j],f[k][i][mid]+mp[mid][j]);
}
}
}
}
return 0;
}
int w33ha(){
scanf("%d%d",&n,&m);
for(int i=0;i<=51;i++)
for(int j=0;j<=51;j++)
val[i][j]=INF;
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
val[u][v]=min(val[u][v],w);
}
pre();
int q;scanf("%d",&q);
while(q--){
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
int ans=INF;
for(int mid=1;mid<=n;mid++){
ans=min(ans,g[k/100][a][mid]+ro[k%100][mid][b]);
}
printf("%d\n",(ans==INF?-1:ans));
}
return 0;
}
int LiangJiaJun(){
int T;scanf("%d",&T);
while(T--)w33ha();
return 0;
}