首页 > 代码库 > hdu 1596 find the safest road(乘积最短路)

hdu 1596 find the safest road(乘积最短路)

题目:

        链接:点击打开链接

题意:

思路:

        对dijkstra稍作修改即可,每次更新dis[]时改为乘积。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 100000000

const int N = 1010;

int n,m;
double map[N][N],dis[N];
int st,ed;

void dijkstra()
{
    int vis[N];
    memset(vis,0,sizeof(vis));
    for(int i=1; i<=n; i++)
    {
        dis[i] = map[st][i];
    }
    for(int i=1; i<n; i++)
    {
        int x;
        double maxx = 0;
        for(int y=1; y<=n; y++)
        {
            if(!vis[y] && (dis[y] - maxx) > 1e-6)
            {
                maxx = dis[x=y];
            }
        }
        vis[x] = 1;
        for(int y=1; y<=n; y++)
        {
            if(maxx*map[x][y] - dis[y] > 1e-6)
                dis[y] = maxx*map[x][y];
        }
    }
    if(dis[ed])
        printf("%.3lf\n",dis[ed]);
    else
        printf("What a pity!\n");
}

int main()
{
    //freopen("input.txt","r",stdin);
    double s;
    while(scanf("%d",&n) != EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%lf",&s);
                map[i][j] = s;
            }
        }
        scanf("%d",&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&st,&ed);
            dijkstra();
        }
    }
    return 0;
}
---------------------------------------------------------------

收获:

---------------------------------------------------------------

战斗,从不退缩;奋斗,永不停歇~~~~~~~~~