106 miles to Chicago
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 3931 | Accepted: 1827 | Special Judge |
Description
In the movie "Blues Brothers", the orphanage where Elwood and Jack were raised may be sold to the Board of Education if they do not pay 5000 dollars in taxes at the Cook Country Assessor's Office in Chicago. After playing a gig in the Palace Hotel ballroom to earn these 5000 dollars, they have to find a way to Chicago. However, this is not so easy as it sounds, since they are chased by the Police, a country band and a group of Nazis. Moreover, it is 106 miles to Chicago, it is dark and they are wearing sunglasses.
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.
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
The input contains several test cases.
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.
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
For each test case, calculate the probability of the safest path from intersection 1 (the Palace Hotel) to intersection n (the Honorable Richard J. Daley Plaza in Chicago). You can assume that there is at least one path between intersection 1 and n.
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.
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
题目就是模板题的简单变形,平时求的是最短的路径,最少的花费啥的,这个求得是能逃脱的最大概率
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 6 using namespace std; 7 const int maxn = 110; 8 const int inf = 0x3f3f3f3f; 9 double graph[maxn][maxn]; 10 int visit[maxn]; 11 double dis[maxn]; 12 int n, m; 13 14 void dijkstra() 15 { 16 for(int i = 2; i <= n; ++i) 17 { 18 dis[i] = graph[1][i]; 19 } 20 visit[1] = 1; 21 dis[1] = 1; 22 23 int k; 24 double max_s; 25 for(int i = 2; i <= n; ++i) 26 { 27 max_s = 0; 28 for(int j = 1; j <= n; ++j) 29 { 30 if(dis[j] > max_s && !visit[j]) 31 { 32 max_s = dis[j]; 33 k = j; 34 } 35 } 36 37 visit[k] = 1; 38 39 for(int j = 1; j <= n; ++j) 40 if(!visit[j] && graph[k][j] * dis[k] > dis[j]) 41 { 42 dis[j] = dis[k] * graph[k][j]; 43 } 44 } 45 } 46 47 int main() 48 { 49 while(cin >> n >> m) 50 { 51 if(n == 0 || m == 0) break; 52 int x, y; 53 double z; 54 memset(graph, 0, sizeof(graph)); 55 for(int i = 1; i <= m; ++i) 56 { 57 cin >> x >> y >> z; 58 graph[x][y] = graph[y][x] = z/100; 59 } 60 memset(visit, 0, sizeof(visit)); 61 dijkstra(); 62 printf("%lf percent ", dis[n]*100); 63 } 64 return 0; 65 }
好的就这。