2018 宁夏邀请赛 F Moving On

Moving On

Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each city has a risk of kidnapping or robbery.

Firdaws’s home locates in the city uu, and Fatinah’s home locates in the city vv.Now you are asked to find the shortest path from the city uu to the city vv that does not pass through any other city with the risk of kidnapping or robbery higher than ww, a threshold given by Firdaws.

Input Format
The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, the first line contains two integers n~(1\le n\le 200)n (1≤n≤200) which is the number of cities, and q~(1\le q\le 2\times 10^4)q (1≤q≤2×10 4 ) which is the number of queries that will be given.The second line contains nn integers r_1, r_2, \cdots, r_nr 1 ,r 2 ,⋯,r n indicating the risk of kidnapping or robbery in the city 11 to nn respectively.Each of the following nn lines contains nn integers, the jj-th one in the ii-th line of which, denoted by d_{i,j}d i,j , is the distance from the city ii to the city jj.Each of the following qq lines gives an independent query with three integers u, vu,v and ww, which are described as above.We guarantee that 1\le r_i \le 10^51≤r i ≤10 5 , 1\le d_{i,j}\le 10^5~(i \neq j)1≤d i,j​ ≤10 5 (i ​ =j), d_{i,i}=0d i,i​ =0 and d_{i,j}=d_{j,i}d i,j =d j,i​ .Besides, each query satisfies 1\le u,v\le n1≤u,v≤n and 1\le w\le 10^51≤w≤10 5 .
Output Format
For each test case, output a line containing Case #x: at first, where xx is the test case number starting from 11.Each of the following qq lines contains an integer indicating the length of the shortest path of the corresponding query.

样例输入

1
3 6
1 2 3
0 1 3
1 0 1
3 1 0
1 1 1
1 2 1
1 3 1
1 1 2
1 2 2
1 3 2

样例输出:
Case #1:
0
1
3
0
1
2

题意:
每个城市由一定的危险值,每次询问问,在危险值不超过w的前提下,最小的路径是多少(不包括起点与终点)

分析:
由于存在200个点,所以可以将其离散化一下,这样就可以得到前n小的排序了,那么定义dp[k][i][j]表示,路过前k小城市从i到j的最小路径
即可得到状态转移方程:
dp[k][i][j]=min(dp[k-1][i][j],dp[k-1][i][id[k]]+dp[k-1][id[k]][j]);
参考:https://blog.csdn.net/qq_40938077/article/details/81205540

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=200+10;
const int INF=0x3f3f3f3f;
vector<int>ve;
int a[N],dp[N][N][N];
int id[N],n,q;
bool cmp(int x,int y){
   return a[x]<a[y];
}
int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    for(int o=1;o<=T;o++){
         printf("Case #%d:\n",o);
        scanf("%d%d",&n,&q);
        memset(dp,INF,sizeof dp);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),id[i]=i;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++) scanf("%d",&dp[0][i][j]);

        sort(id+1,id+1+n,cmp);
//        for(int i=1;i<=n;i++)
//            cout<<id[i]<<" ";
//        cout<<endl;

        for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    dp[k][i][j]=min(dp[k-1][i][j],dp[k-1][i][id[k]]+dp[k-1][id[k]][j]);
                }
            }
        }

        int ans=INF;
        while(q--){int u,v, w;
            scanf("%d%d%d",&u,&v,&w);
            ans=INF;
            for(int i=0;i<=n;i++)
                if(a[id[i]]<=w){
                    ans=min(ans,dp[i][u][v]);
                }

            printf("%d\n",ans);
        }
    }
    return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值