| Find the safest road |
| Time limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others) |
| Total submission (s): 136 Accepted Submission (s): 64 |
|
| Problem Descriptionxx Planet There are many cities, each city has one or more flight lanes, but not all roads are very safe, each road has a safety factor s,s is the real number between 0 and 1 (including 0, 1), a channel p from u to V The safety is safe (P) = S (E1) *s (E2) ... *s (EK) E1,e2,ek is on the side of P, now 8600 want to go out to travel, faced with so many roads, he wanted to find the safest way. But 8600 of the math is not good, I would like to ask you to help ^_^ |
Input inputs include multiple test instances, each of which includes: First line: N. n indicates the number of cities n<=1000; Then there is a n*n matrix that represents the safety factor between the two cities (0 can be understood as there is no direct channel between the two cities) followed by the Q 8600 route to travel, each line has two numbers, indicating the city of 8600 and the city to go |
Output If 86 fails to reach his destination, outputs "What a pity!", The other outputs the safety factor of the safest road between these two cities and retains three decimal places. |
Sample Input31 0.5 0.50.5 1 0.40.5 0.4 131 22 31 3 |
Sample Output0.5000.4000.500 |
| Authorailyanlu |
| Sourcehdu 2007-spring Programming Contest-warm up (1) |
| Recommend8600 |
#include <bits/stdc++.h>#defineINF 0x3f3f3f3f#defineN 1010using namespacestd;intN;DoubleMapn[n][n];DoubleDis[n];intVis[n];voidDijkstraints) { intI,j,pos; memset (Vis,0,sizeof(VIS)); for(i =1; i<=n; i++) Dis[i]=Mapn[s][i]; Dis[s]=1; Vis[s]=1; POS=s; DoubleMAXN; for(i =1; i<=n; i++) {MAXN=0; for(j =1; j<=n; J + +){ if(DIS[J]>MAXN &&!)Vis[j]) {POS=J; MAXN=Dis[j]; }} Vis[pos]=1; for(j =1; j<=n; J + +){ if(Dis[pos]*mapn[pos][j]>dis[j] &&!Vis[j]) dis[j]= dis[pos]*Mapn[pos][j]; } }}intm;intx, y;intMain () {//freopen ("C:\\users\\acer\\desktop\\in.txt", "R", stdin); while(SCANF ("%d", &n)! =EOF) { /*Initialize*/ for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) {scanf ("%LF",&Mapn[i][j]); }} scanf ("%d",&m); while(m--) {scanf ("%d%d",&x,&y); Dijkstra (x); if(dis[y]<0.0000001) puts ("What a pity!"); Elseprintf"%.3lf\n", Dis[y]); } } return 0;}
Find the safest road