zoukankan      html  css  js  c++  java
  • POJ 2472 ||SDUT 2358 106 miles to Chicago(Dijkstra算法变形)

    106 miles to Chicago

    Time Limit: 2000MS

     

    Memory Limit: 65536K

    Total Submissions: 2765

     

    Accepted: 1328

     

    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

    Ulm Local 2005

     解题报告:这道题就是最短路径的变形题,相当于求最大路径,题意就是给出我们两点之间不被抓住的概率,求到达n的最大不被抓住的概率!这道题变态的是用G++提交WA用C++提交就AC!哎!WA了十几次

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    const int MAX = 105;
    double map[MAX][MAX], dis[MAX];
    int visit[MAX];//标记变量
    int n, m;
    void Dijkstra()
    {
        int i, j, p;
        for (i = 1; i <= n; ++i)//初始化
        {
            dis[i] = map[1][i];
        }
        visit[1] = 1;
        double max;
        for (i = 1; i <= n; ++i)
        {
            max = -1.0;
            for (j = 1; j <= n; ++j)
            {
                if (!visit[j] && dis[j] > max)
                {
                    p = j;
                    max = dis[j];
                }
            }
            visit[p] = 1;
            for (j = 1; j <= n; ++j)
            {
                if (!visit[j] && dis[j] < dis[p] * map[p][j])//更新
                {
                    dis[j] = dis[p] * map[p][j];
                }
            }
        }
    }
    int main()
    {
        int i, j, x, y;
        double v;
        while (scanf("%d", &n) != EOF && n)
        {
            scanf("%d", &m);
            memset(map, 0, sizeof(map));
            memset(visit, 0, sizeof(visit));
            memset(dis, 0, sizeof(dis));
            for (i = 0; i < m; ++i)
            {
                scanf("%d%d%lf", &x, &y, &v);
                map[x][y] = map[y][x] = v / 100.0;
            }
            Dijkstra();
            printf("%.6lf percent\n", dis[n] * 100.0);
        }
        return 0;
    }
  • 相关阅读:
    CSS display:none和visibility:hidden区别
    Eclipse 开发版本选择
    JavaScript 三种弹出框
    jstree的数据后台生成
    C# Enum,Int,String的互相转换 枚举转换
    JS 异常: Uncaught RangeError: Maximum call stack size exceeded
    JS中encodeURI,escape,encodeURIComponent区别
    安卓Android手机系统内文件夹目录解释
    jquery操作select(取值,设置选中)
    单例/单体模式(Singleton)
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2454494.html
Copyright © 2011-2022 走看看