zoukankan      html  css  js  c++  java
  • HDOJ find the safest road 1596【最短路变形】



    find the safest road

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


    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
     

    Source
     

    Recommend
    8600   |   We have carefully selected several similar problems for you:  1217 1598 1142 1690 1385 
     


    最短路变形,把求最小改成最大就可以。

    注意在Q次询问中会发生权值改变,所以我们须要定义一个数组来保存权值,询问之前把值又一次赋给权值

    #include <stdio.h>
    #include <math.h>
    #include <vector>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <algorithm>
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int MAXN = 1010;
    bool used[MAXN];
    double d[MAXN];
    double cost[MAXN][MAXN];
    double a[MAXN][MAXN];
    int N;
    
    void Dijkstra(int s)
    {
        for(int i=0;i<N;i++){
            used[i]=false;
            d[i]=0;
        }
        d[s]=1;
        while(true){
            int v=-1;
            for(int i=0;i<N;i++)
                if(!used[i]&&(v==-1||d[v]<d[i])) v=i;
            if(v==-1) break;
            used[v]=true;
            for(int i=0;i<N;i++)
                d[i]=max(d[i],d[v]*cost[v][i]);
        }
    }
    int main()
    {
        while(scanf("%d",&N)!=EOF){
            for(int i=0;i<N;i++){
                for(int j=0;j<N;j++){
                    scanf("%lf",&a[i][j]);
                    //if(a[i][j]==0) a[i][j]=0;
                }
            }
            int Q;
            scanf("%d",&Q);
            int s,g;
            while(Q--){
                //memcpy(cost,a,sizeof(a));//能够取代以下的for循环。可是耗时
                for(int i=0;i<N;i++){
                    for(int j=0;j<N;j++)
                        cost[i][j]=a[i][j];
                }
                scanf("%d%d",&s,&g);
                Dijkstra(s-1);
                if(d[g-1]==0) printf("What a pity!
    ");
                else printf("%.3lf
    ",d[g-1]);
            }
        }
        return 0;
    }
    



  • 相关阅读:
    Idea导入tomcat源码
    SpringBoot学习 (一) Eclipse中创建新的SpringBoot项目
    Zookeeper客户端Curator使用详解
    解决老是提示找不到Mapper文件无法执行定义的方法问题!
    人工智能、机器学习、深度学习三者之间的关系
    java5增加对https的支持
    Spring静态注入的三种方式
    Thrift入门及Java实例演示【转】
    activeMQ 学习
    python中 import 和from ... import 的区别
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6979504.html
Copyright © 2011-2022 走看看