zoukankan      html  css  js  c++  java
  • HDU 1596 find the safest road(SPFA)

    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
     
    这道题目赋初值的时候注意一下就行
     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<limits.h>
     5 #include<queue>
     6 using namespace std;
     7 int n,m;
     8 int s,e;
     9 double a[1005][1005];
    10 double d[1005];
    11 bool vis[1005];
    12 void spfa(int i)
    13 {
    14     queue<int>Q;
    15     Q.push(i);
    16     vis[i]=true;
    17     int cur;
    18     while(!Q.empty())
    19     {
    20         cur=Q.front();
    21         Q.pop();
    22         vis[cur]=0;
    23         int j;
    24         for(j=1;j<=n;j++)
    25         {
    26             if(j!=cur && a[cur][j]*d[cur]>d[j] && a[cur][j]>0)
    27             {
    28                 d[j]=a[cur][j]*d[cur];
    29                 if(!vis[j])
    30                 {
    31                     vis[j]=true;
    32                     Q.push(j);
    33                 }
    34             }
    35         }
    36     }
    37     return;
    38 }
    39 int main()
    40 {
    41     int i,j;
    42     while(cin>>n)
    43     {
    44         for(i=1;i<=n;i++)
    45             for(j=1;j<=n;j++)
    46                 cin>>a[i][j];
    47         cin>>m;
    48         while(m--)
    49         {
    50             cin>>s>>e;
    51             memset(vis,false,sizeof(vis));
    52             memset(d,0,sizeof(d));
    53             d[s]=a[s][s];
    54             spfa(s);
    55              
    56             if(d[e]!=0) printf("%.3lf
    ",d[e]);
    57             else  cout<<"What a pity!"<<endl;
    58         }
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/Annetree/p/5682311.html
Copyright © 2011-2022 走看看