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 INPUT3 1 0.5 0.5 0.5 1 0.4 0.5 0.4 1 3 1 2 2 3 1 3
Sample Output0.500 0.400 0.500 ideas: The algorithm or the use of Dijkstra algorithm, as long as the change to the maximum distance is good, to pay attention to the type of number
1#include <stdio.h>2#include <algorithm>3 intN, Q;4 Doublesafe[1010], sa[1010][1010];5 intvis[1010];6 DoubleMaxDoubleXDoubley)7 {8 returnx > y?x:y;9 }Ten voidDijkstraintSintt) One { A intu, v; - for(U =1; U <= N; u++) - { theVis[u] =0; -Safe[u] =0.0; - } -Safe[s] =1.0; + while(true) - { +v=-1; A for(U =1; U <= N; u++) at if(!vis[u] && (safe[u]>safe[v) | | v==-1)) -v =u; - if(v = =-1) - Break; -VIS[V] =1; - for(U =1; U <= N; u++) in { -Safe[u] = max (Safe[u], safe[v]*Sa[v][u]); to } + } - if(Safe[t] = =0.0) theprintf"What a pity!\n"); * Else $printf"%.3f\n", Safe[t]);Panax Notoginseng } - intMain () the { + Doubles; A while(~SCANF ("%d", &N)) the { + for(inti =1; I <= N; i++) - for(intj =1; J <= N; J + +) $ { $scanf"%LF", &s); - if(I <=j) -SA[I][J] = Sa[j][i] =s; the } -scanf"%d", &q);Wuyi while(q--) the { - intA, B; Wuscanf"%d%d", &a, &b); - Dijkstra (A, b); About } $ } - return 0; -}
Hdoj 1596 find the safest rode