zoukankan      html  css  js  c++  java
  • 宁夏邀请赛F FLOYD

    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~(1le nle 200)n (1n200) which is the number of cities, and q~(1le qle 2 imes 10^4)q (1q2×104) which is the number of queries that will be given.The second line contains nn integers r_1, r_2, cdots, r_nr1,r2,,rn indicating the risk of kidnapping or robbery in the city 11 to nn respectively.Each of the following nnlines contains nn integers, the jj-th one in the ii-th line of which, denoted by d_{i,j}di,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 1le r_i le 10^51ri105, 1le d_{i,j}le 10^5~(i eq j)1di,j105 (ij), d_{i,i}=0di,i=0 and d_{i,j}=d_{j,i}di,j=dj,i.Besides, each query satisfies 1le u,vle n1u,vn and 1le wle 10^51w105.

    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

    题目来源

    The 2018 ACM-ICPC Chinese Collegiate Programming Contest

     

    思路:动态规划f[i][j][k]记录经过前i个城市从j到k的最小风险,再将每个城市的风险进行排序,用于转移与计算。

    代码:

     1 #include"bits/stdc++.h"
     2 #define ci(x) scanf("%d",&x)
     3 const int N   = 1e6 + 5;
     4 using namespace std;
     5 int t,n,q;
     6 struct P
     7 {
     8     int id,r;
     9 };
    10 P a[N];
    11 int f[205][205][205];
    12 bool cmp(P a,P b){
    13     return a.r<b.r;
    14 }
    15 void cal(int m){
    16     for(int i=1;i<=m;i++){
    17         int id=a[i].id;
    18         for(int j=1;j<=n;j++){
    19             for(int k=1;k<=n;k++){
    20                 f[i][j][k]=min(f[i-1][j][k],f[i-1][j][id]+f[i-1][id][k]);
    21             }
    22         }
    23     }
    24 }
    25 int main(){
    26     //freopen("data.in","r",stdin);
    27     //freopen("data.out","w",stdout);
    28     scanf("%d",&t);
    29     for(int ii=1;ii<=t;ii++){
    30         scanf("%d%d",&n,&q);
    31         for(int i=1;i<=n;i++) ci(a[i].r),a[i].id=i;
    32         memset(f,inf,sizeof(f));
    33         for(int i=1;i<=n;i++)
    34             for(int j=1;j<=n;j++) ci(f[0][i][j]);
    35         sort(a+1,a+n+1,cmp);
    36         cal(n);
    37         printf("Case #%d:
    ",ii);
    38         int u,v,w;
    39         for(int i=0;i<q;i++){
    40             ci(u),ci(v),ci(w);
    41             int k;
    42             for(k=1;k<=n;k++) if(a[k].r>w) break;
    43             printf("%d
    ",f[k-1][u][v]);
    44         }
    45     }
    46     return 0;
    47 }

     

  • 相关阅读:
    POJ 1258 Agri-Net (prim水题)
    POJ 1258 Agri-Net (prim水题)
    Call to your teacher
    Call to your teacher
    Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注)
    用R语言求置信区间
    用R语言求置信区间
    竞品分析报告正确的打开方式
    竞品分析报告正确的打开方式
    内容分析在用户反馈分析中的应用
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9435322.html
Copyright © 2011-2022 走看看