• HDU 1596 find the safest road


    find the safest road

    Time Limit: 5000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1596
    64-bit integer IO format: %I64d      Java class name: Main
     
    XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 ^_^
     

    Input

    输入包括多个测试实例,每个实例包括:
    第一行:n。n表示城市的个数n<=1000;
    接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
    接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
     

    Output

    如果86无法达到他的目的地,输出"What a pity!",
    其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
     

    Sample Input

    3
    1 0.5 0.5
    0.5 1 0.4
    0.5 0.4 1
    3
    1 2
    2 3
    1 3

    Sample Output

    0.500
    0.400
    0.500

    Source

     
    解题:貌似用单源最短路路效率更高。Floyd险过。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 1010;
    18 double mp[maxn][maxn];
    19 int n,m;
    20 int main() {
    21     int i,j,k;
    22     while(~scanf("%d",&n)){
    23         for(i = 1; i <= n; i++){
    24             for(j = 1; j <= n; j++)
    25                 scanf("%lf",mp[i]+j);
    26         }
    27         for(k = 1; k <= n; k++){
    28             for(i = 1; i <= n; i++){
    29                 for(j = 1; j <= n; j++){
    30                     double ret = mp[i][k]*mp[k][j];
    31                     if(ret > mp[i][j]) mp[i][j] = ret;
    32                 }
    33             }
    34         }
    35         scanf("%d",&m);
    36         for(k = 0; k < m; k++){
    37             scanf("%d %d",&i,&j);
    38             if(mp[i][j] == 0) puts("What a pity!");
    39             else printf("%.3f
    ",mp[i][j]);
    40         }
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    OCP 062【中文】考试题库(cuug内部资料)第29题
    413. 等差数列划分 力扣(中等) 找规律,细节
    264. 丑数 II 力扣(中等) 动态规划,不会
    313. 超级丑数 力扣(中等) 动态规划,不会做
    5840. 使字符串平衡的最小交换次数 力扣(中等) 第255场oppo周赛 猜出来的
    手写一个仿微信登录的Nodejs程序
    你不知道的CSS国际化
    React实现类似淘宝tab居中切换效果
    原来 CSS 这样写是会让 App 崩溃的
    css中class和id之间有什么区别?
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3944832.html
Copyright © 2020-2023  润新知