Problem M. Walking Plan hdu6331(最短路)

Problem M. Walking Plan
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 375 Accepted Submission(s): 128

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

题意:每次查询,求出u到v至少经过k条边的最短路

思路:看了大神的思路,这题容易想到dp[i][j][k] 表示从i到j走了k条边的最短路,转移也很很简单,floyd加多一维就行了,但是这里的k有10000,显然不行,但是我们可以定义个辅助数组
dp2[i][j][k] 表示i到j走了k*100条路的最短路,转移可以通过dp1来转移,最后一点就是题目要求
最少k条路的最短路,那么我们在定义一个dp3[i][j][k] 代表从i到i走了至少k条路的最短路,那么转移就是 dp3[i][j][k] = min(dp3[i][j][k],dp1[i][w][k]+dis[w][j]) dis为w到j的最短路

accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
const int maxn = 1e7+4;
using namespace std;
LL mod;
LL dp1[52][52][100+4];
LL dp2[52][52][100+5];
LL dp3[52][52][100+42];
int T;
LL dis[55][55];
LL d[55][55];
int n,m;
int q;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(d,0x3f,sizeof(d));
        for(int i = 1; i<=m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            d[u][v] = min(d[u][v],(LL)w);
        }
        for(int i = 1; i<=n; i++)
        {
            for(int j = 1; j<=n; j++)
            {
                dis[i][j] = d[i][j];
            }
            dis[i][i] = 0;
        }
        for(int k = 1; k<=n; k++)
        {
            for(int i = 1; i<=n; i++)
            {
                for(int j = 1; j<=n; j++)
                {
                    dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
        }

        memset(dp1,0x3f,sizeof(dp1));
        for(int i = 1;i<=n;i++){
            dp1[i][i][0] = 0;
        }
        for(int k = 1;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp1[j][z][k]=min(dp1[j][z][k],dp1[j][i][k-1]+d[i][z]);
                    }
                }
            }
        }

        memset(dp2,0x3f,sizeof(dp2));
        for(int i = 1;i<=n;i++){
            dp2[i][i][0] = 0;
        }
        for(int k = 1;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp2[j][z][k]=min(dp2[j][z][k],dp2[j][i][k-1]+dp1[i][z][100]);
                    }
                }
            }
        }

        memset(dp3,0x3f,sizeof(dp3));
        for(int k = 0;k<=100;k++){
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int z = 1;z<=n;z++){
                        dp3[j][z][k]=min(dp3[j][z][k],dp1[j][i][k]+dis[i][z]);
                    }
                }
            }
        }

        scanf("%d",&q);
        while(q--){
            int u,v,k;
            scanf("%d%d%d",&u,&v,&k);
            int t1 = k/100;
            int t2 = k%100;
            LL ans = INF;
            for(int i = 1;i<=n;i++){
                ans = min(ans,dp2[u][i][t1]+dp3[i][v][t2]);
            }
            if(ans>=INF){
                puts("-1");
                continue;
            }
            printf("%lld\n",ans);
        }
    }
}


猜你喜欢

转载自blog.csdn.net/w571523631/article/details/81301616