1. 程式人生 > >hdu 1596 find the safest road

hdu 1596 find the safest road

esp return clu include using one 旅遊 space ott

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!",
其他的輸出這兩個城市之間的最安全道路的安全系數,保留三位小數。 直接dij就好了,惡心在double,一沒註意用了int就wa了 技術分享
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
typedef long long LL;

int visited[1005],n,q;
double safety[1005][1005];
double dest[1005];
int main(){
  
// freopen("test.in","r",stdin); while (scanf("%d",&n) != EOF){ for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++){ scanf("%lf",&safety[i][j]); } } scanf("%d",&q); for (int i=1;i<=q;i++){ int src,des; memset(visited,0,sizeof(visited)); memset(dest,
0,sizeof(dest)); scanf("%d %d",&src,&des); for (int j=1;j<=n;j++){ dest[j] = safety[src][j]; } dest[src] = 1; visited[src] = 1; int finish = 0; while (!finish){ int maxi = 0; double maxdes = -1; for (int j=1;j<=n;j++){ // cout << dest[j] << " "; if (!visited[j] && dest[j] > maxdes){ maxi = j; maxdes = dest[j]; } } // cout << endl; if (maxi == 0){ finish = 1; break; } visited[maxi] = 1; for (int j=1;j<=n;j++){ if (!visited[j]){ dest[j] = max(dest[j],dest[maxi] * safety[maxi][j]); } } } if (dest[des] != 0) printf("%.3lf\n",dest[des]); else printf("What a pity!\n"); } } return 0; }
View Code

hdu 1596 find the safest road