zoukankan      html  css  js  c++  java
  • hdu 1596 find the safest road (dijkstra)



    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
     

    #include<queue>
    #include<stack>
    #include<vector>
    #include<math.h>
    #include<stdio.h>
    #include<numeric>//STL数值算法头文件
    #include<stdlib.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<functional>//模板类头文件
    using namespace std;
    
    const long long INF=1e9+7;
    const int maxn=1010;
    
    int n,m,st,ed;
    double tu[maxn][maxn],degeree[maxn];
    int vis[maxn];
    
    void dijkstra(int st,int ed)
    {
        int i,j;
        for(i=1; i<=n; i++)
        {
            degeree[i]=tu[st][i];
            vis[i]=0;
        }
        vis[st]=1;
        bool flag=0;
        double maxx;
        for(i=1; i<n; i++)
        {
            int k;
            maxx=0;
            for(j=1; j<=n; j++)
            {
                if(!vis[j]&°eree[j]>maxx)
                {
                    k=j;
                    maxx=degeree[j];
                }
            }
            if(fabs(maxx)<10e-6)
            {
                flag=1;
                break;
            }
            vis[k]=1;
            for(j=1; j<=n; j++)
            {
                if(!vis[j]&&tu[k][j]>0&°eree[j]<tu[k][j]*degeree[k])
                {
                    degeree[j]=degeree[k]*tu[k][j];
                }
            }
        }
        if(flag) printf("What a pity!
    ");
        else printf("%.3lf
    ",degeree[ed]);
    }
    
    int main()
    {
        int i,j;
        while(~scanf("%d",&n))
        {
            for(i=1; i<=n; i++)
                for(j=1; j<=n; j++)
                    scanf("%lf",&tu[i][j]);
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d %d",&st,&ed);
                dijkstra(st,ed);
            }
        }
        return 0;
    }
     

  • 相关阅读:
    访存模型
    Petri网
    Forward secrecy
    TensorFlow训练神经网络cost一直为0
    解决tensorflow在训练的时候权重是nan问题
    tf.argmax
    Keras教程
    z-score
    隐马尔可夫(HMM)、前/后向算法、Viterbi算法
    受限玻尔兹曼机基础教程
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264866.html
Copyright © 2011-2022 走看看