• HDU Problem 1596 find the safest road【最短路dijkstra】


    find the safest road

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 11511    Accepted Submission(s): 4058

    Problem Description
    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
     
    Author
    ailyanlu
     
    题意:从v到u的安全系数为所经过路上的e的乘积。按照最短路,每次选取乘积最大的保存,要是最终的乘积为零,说明到不了。

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #define MAXN 1005
    using namespace std;
    const int INF = 1e9;
    int n, m;
    bool vis[MAXN];
    double  d[MAXN], cost[MAXN][MAXN];
    void dijkstra(int x) {
        for (int i = 0; i <= n; i++) {
            d[i] = 0;
        }
        d[x] = 1;
        memset(vis, false, sizeof(vis));
        while (true) {
            int v = -1;
            for (int u = 1; u <= n; u++) {
                if (!vis[u] && (v==-1||d[u]>d[v])) v=u;
            }
            if (v == -1) break; vis[v] = true;
            for (int u = 1; u <= n; u++) {
                d[u] = max(d[u], d[v]*cost[v][u]);
            }
        }
    }
    int main() {
        int a, b, c;
        while (scanf("%d", &n) != EOF) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    scanf("%lf", &cost[i][j]);
                }
            }
            scanf("%d", &m);
            for (int i = 1; i <= m; i++) {
                scanf("%d%d", &a, &b);
                dijkstra(a);
                if (d[b] == 0) printf("What a pity!
    ");
                else printf("%.3lf
    ", d[b]);
            }
        }
        return 0;
    }

  • 相关阅读:
    优化后的组合算法
    Android之——AsyncTask和Handler对照
    LeetCode(30) Substring with Concatenation of All Words
    HDU--1054--Strategic Game【最小点覆盖】
    最小生成树模板(poj3625)
    QT中使用高速排序
    走进APICloud的世界 (1)
    layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
    移动端H5的一些基本知识点总结
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770863.html
Copyright © 2020-2023  润新知