POJ 2472 106 miles to Chicago

106 miles to Chicago
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3305   Accepted: 1557   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.

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.

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.

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

Source

 
 
题意:Elwood和Jack要从the Palace Hotel(顶点1)尽量躲避警察的追捕,驾车到Chicago(顶点n)。现在有n个顶点,m条边,给出在每条边上不被警察追捕到的几率,问最终Elwood和Jack能安全到达Chicago而不被警察追捕到的最大概率是多少。

思路:逆向的dijkstra 要求的不是最小的,而是最大的。
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=110;
const int INF=0x3f3f3f3f;

int n,m;
double map[N][N],dis[N];
int vis[N];

void Dijkstra(int src){
    int i;
    for(i=1;i<=n;i++){
        dis[i]=map[src][i]; //初始化路径概率
        vis[i]=0;
    }
    dis[src]=1; //本身的概率是1
    int j,k;
    double tmp;
    for(i=1;i<n;i++){
        tmp=0;
        for(j=1;j<=n;j++)
            if(!vis[j] && tmp<dis[j]){  //找出概率最大的
                tmp=dis[j];
                k=j;
            }
        if(tmp==0)
            break;
        vis[k]=1;
        for(j=1;j<=n;j++)
            if(!vis[j] && dis[j]<dis[k]*map[k][j])   //更新路径概率
                dis[j]=dis[k]*map[k][j];
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d",&n) && n){
        scanf("%d",&m);
        memset(map,0,sizeof(map));
        int a,b,p;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&p);
            map[a][b]=map[b][a]=p/100.0;
        }
        Dijkstra(1);
        printf("%.6lf percent\n",dis[n]*100);
    }
    return 0;
}

 

posted @ 2013-04-23 14:41  Jack Ge  阅读(487)  评论(0编辑  收藏  举报