Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 4064 | Accepted: 1876 | Special Judge |
Description
As they are on a mission from God, you should help them find the safest way to Chicago. In this problem, the safest way is considered to be the route which maximises the probability that they are not caught.
Input
Each test case starts with two integers n and m (2 <= n <= 100 , 1 <= m <= n*(n-1)/2). n is the number of intersections, m is the number of streets to be considered.
The next m lines contain the description of the streets. Each street is described by a line containing 3 integers a, b and p (1 <= a, b <= n , a != b, 1 <= p <= 100): a and b are the two end points of the street and p is the probability in percent that the Blues Brothers will manage to use this street without being caught. Each street can be used in both directions. You may assume that there is at most one street between two end points.
The last test case is followed by a zero.
Output
Print the probability as a percentage with exactly 6 digits after the decimal point. The percentage value is considered correct if it differs by at most 10-6 from the judge output. Adhere to the format shown below and print one line for each test case.
Sample Input
5 7 5 2 100 3 5 80 2 3 70 2 1 50 3 4 90 4 1 85 3 1 70 0
Sample Output
61.200000 percent
注意:i==j,dist[i][j]=1.0;
不連通,dist[i][j] = 0;
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
double dist[105][105];
const double eps = 1e-6;
void init(int n) {
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
dist[i][j] = 0;
if (i == j) dist[i][j] = 1.0;
}
}
}
void floyd(int n) {
for (int k = 1; k <= n; k ++) {
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (dist[i][j] - (dist[i][k]*dist[k][j]) < eps) {
dist[i][j] = dist[i][k]*dist[k][j];
}
}
}
}
}
int main() {
int n, m;
int x, y;
double p;
while (scanf("%d", &n), n) {
scanf("%d", &m);
init(n);
for (int i = 0; i < m; i ++) {
scanf("%d%d%lf", &x, &y, &p);
dist[x][y] = dist[y][x] = p/100;
}
floyd(n);
printf("%.6f percent\n", dist[1][n]*100);
}
return 0;
}