【分块dp】HDU 6331 Problem M. Walking Plan

【分块dp】HDU 6331 Problem M. Walking Plan

【题目链接】


题目大意

T组数据
每组n个点m条边,给一个有向图,边有长度。
接下来Q个询问
每次问u到v至少经过x个点的最短距离

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

解题思路

题目中要求至少经过的点的数量不超过10000,所以分成100*100做。
dpa[t][i][j]表示从i到j恰好经过t个点的最短路径,t<=100;
dpb[t][i][j]表示从i到j恰好经过t*100个点的最短路径,t<=100;
dpc[t][i][j]表示从i到j至少经过t个点的最短距离,t<=100;
dis[i][j]表示从i到j的最短路;//floyed预处理


AC代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn = 57;
const int maxt = 107;
const LL mod = 1e18+7;
const LL INF=1e15;

LL dpa[maxt][maxn][maxn];
LL dpb[maxt][maxn][maxn];
LL dpc[maxt][maxn][maxn];
LL edge[maxn][maxn];
LL dis[maxn][maxn];
int main()
{
    LL T,n,m,i,j,k,x,y,u,v,Q,t,ans;
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                edge[i][j]=INF;
                if(i!=j) dis[i][j]=INF;
            }
        while(m--)
        {
            cin>>u>>v>>x;
            edge[u][v]=min(edge[u][v],x);
            dis[u][v]=edge[u][v];
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(i!=j) {dpa[0][i][j]=dpb[0][i][j]=
                    dpc[0][i][j]=INF;}
            }
        }
        for(t=1;t<=100;t++)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    dpa[t][i][j]=INF;
                    for(k=1;k<=n;k++)
                    {
                        dpa[t][i][j]=min(dpa[t][i][j],dpa[t-1][i][k]+edge[k][j]);
                    }
                }
            }
        }
        for(t=1;t<=100;t++)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    dpb[t][i][j]=INF;
                    for(k=1;k<=n;k++)
                    {
                        dpb[t][i][j]=min(dpb[t][i][j],dpb[t-1][i][k]+dpa[100][k][j]);
                    }
                }
            }
        }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                for(k=1;k<=n;k++)
                    dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
        for(t=0;t<=100;t++)///注意这里是从0开始
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    dpc[t][i][j]=dpa[t][i][j];
                    for(k=1;k<=n;k++)
                    {
                        dpc[t][i][j]=min(dpc[t][i][j],dpa[t][i][k]+dis[k][j]);
                    }
                }
            }
        }
        cin>>Q;
        while(Q--)
        {
            cin>>u>>v>>x;
            ans=INF;
            for(i=1;i<=n;i++)
            {
                ans=min(ans,dpb[x/100][u][i]+dpc[x%100][i][v]);
            }
            if(ans<INF) cout<<ans<<endl;
            else cout<<"-1\n";
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值