DescriptionXX Planet has many cities, there is one or more flight lanes between each city, but not all roads are safe, and each road has a safety factor s,s is a real number between 0 and 1 (including 0, 1), and the safety of a channel P from U to V is safe (P) = S (E1) *s (E2) ... * S (EK) E1,e2,ek is the side of P, now 8600 want to go out to travel, in the face of so many roads, he wants to find the safest way. But 8600 of the math is not good, I would like to ask you to help ^_^InputThe input includes 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 goOutputIf 86 fails to reach his destination, output "What a pity!",
The other outputs the safety factor of the safest road between these two cities and retains three decimal places.Sample Input
31 0.5 0.50.5 1 0.40.5 0.4 131 22 31 3
Sample Output
0.5000.4000.500
The shortest path used before is the Min function initialization array map[][] is positive infinity, the problem of maximum security to initialize the array with the MAX function is negative infinity.
1#include <cstdio>2#include <algorithm>3 #defineInf-0xfffffff4 using namespacestd;5 intN;6 Doublemap[1005][1005];7 voidF1 ()8 {9 intk,i,j;Ten for(k =1; K <= N; k++) One { A for(i =1; I <= N; i++) - { - for(j =1; J <= N; J + +) the { -Map[i][j]=max (Map[i][j],map[i][k]*map[k][j]);//calculate the security record for every two points is a high degree of security - } - } + } - } + intMain () A { at DoubleA; - intb,c,i,j,q; - while(SCANF ("%d", &n)! =EOF) - { - for(i =1; I <= N; i++) - { in for(j =1; J <= N; j + +) - { tomap[i][j]= (i = = j)?1: INF;//I=j record 1 otherwise record negative infinity + } - } the for(i =1; I <= N; i++) * { $ for(j =1; J <= N; J + +)Panax Notoginseng { -scanf"%LF",&a); the if(I <j) + { AMap[i][j]=map[j][i]=a;//record the security of every two points, in order to simply record the situation of I<j the } + } - } $ F1 (); $scanf"%d",&q); - while(q--) - { thescanf"%d%d",&b,&c); - if(Map[b][c] >0)//if the two points are different, the safety degree must be 0 or greater than 0.Wuyiprintf"%.3f\n", Map[b][c]); the Else -printf"What a pity!\n"); Wu } - } About}
Hangzhou Electric 1596 Find the safest road (minimum path deformation for maximum safety)