HDU 6331 Walking Plan(最短路+dp+分块思想)

Problem M. Walking Plan

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 897    Accepted Submission(s): 328


 

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

 题目大意:给出n个点和m条边,给出边的关系,解决q次询问,每次询问是问u点到v点至少走k条边的最短路

仔细想一想觉得很巧妙,我们可以设几个dp数组,dp[k][i][j]表示恰好走k条边从i点走到j点,这样跑一百遍,就可以得出恰好走100条边从i点走到j点,询问中的边的个数是10000的,所以就分成组,我们用dp1数组来存,dp1[k][i][j]就表示走100k条边从i点到j点,处理完这个,再用最开始存边的邻接矩阵跑一个floyd,为什么要跑floyd呢,因为在前面我们走的是恰好k条边,如果在前面就跑floyd,那么会不知道到底走了多少条边,而题目问的是至少,所有假如要101条边,那么我们已经处理了100条边的情况,剩下的就是至少一条边了,而用floyd怎样跑出的最短路都会大于一条。(也不知道理解的对不对)

最后就可以处理询问了

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=60;
const int inf=0x3f3f3f3f;
int map[maxn][maxn];
int dp[110][maxn][maxn];
int dp1[110][maxn][maxn];
int dp2[110][maxn][maxn];
int main()
{
	//freopen("in.txt","r",stdin);
    int test;
    scanf("%d",&test);
    while(test--)
    {
    	memset(map,inf,sizeof(map));
    	memset(dp,inf,sizeof(dp));
        memset(dp1,inf,sizeof(dp1));
        memset(dp2,inf,sizeof(dp2));
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            map[u][v]=min(map[u][v],w);
        }
        for(int i=1;i<=n;i++)
        {
            dp[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 a=1;a<=n;a++)
                    {
                        dp[k][i][j]=min(dp[k][i][j],dp[k-1][i][a]+map[a][j]);
                    }
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            dp1[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 a=1;a<=n;a++)
                    {
                        dp1[k][i][j]=min(dp1[k][i][j],dp1[k-1][i][a]+dp[100][a][j]);
                    }
                }
            }
        }
        for(int i=1;i<=n;i++)
		{
			map[i][i]=0;
		}
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
                }
            }
        }
        for(int k=0;k<=100;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    for(int a=1;a<=n;a++)
                    {
                        dp2[k][i][j]=min(dp2[k][i][j],dp[k][i][a]+map[a][j]);
                    }
                }
            }
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int a,b,k;
            scanf("%d%d%d",&a,&b,&k);
            int k1=k/100;
            int k2=k%100;
            int ans=inf;
            for(int i=1;i<=n;i++)
            {
                ans=min(ans,dp1[k1][a][i]+dp2[k2][i][b]);
            }
            if(ans==inf) puts("-1");
            else printf("%d\n",ans);
        }
    }
    return 0;
}
/*
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
*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值