hdu1569find the safest road(floyd變形求最大安全值),hdu1569findsafest

來源:互聯網
上載者:User

hdu1569find the safest road(floyd變形求最大安全值),hdu1569findsafest

find the safest roadTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7845    Accepted Submission(s): 2786


Problem DescriptionXX星球有很多城市,每個城市之間有一條或多條飛行通道,但是並不是所有的路都是很安全的,每一條路有一個安全係數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
31 0.5 0.50.5 1 0.40.5 0.4 131 22 31 3
 
Sample Output
0.5000.4000.500
 

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespace std;#define maxn 1005#define inf 0x3f3f3f3fdouble  d[maxn][maxn];int n;void floyd(){    for(int k=1;k<=n;k++)    for(int i=1;i<=n;i++)    for(int j=1;j<=n;j++){        d[i][j]=max(d[i][j],d[i][k]*d[k][j]);    }}int main(){    int q;    int a,b;   // freopen("in.txt","r",stdin);    while(~scanf("%d",&n)&&n){        memset(d,inf,sizeof d);        for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)            scanf("%lf",&d[i][j]);        floyd();        scanf("%d",&q);        for(int i=0;i<q;i++){            scanf("%d%d",&a,&b);            if(d[a][b])            printf("%.3lf\n",d[a][b]);            else puts("What a pity!");        }    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.