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;
}

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

收获:

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

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

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

时间: 2024-08-25 07:23:12

hdu 1596 find the safest road(乘积最短路)的相关文章

hdu 1596 find the safest road(最短路)

hdu 1596 find the safest road Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)-*s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路.但是8600 的数学不好,想请你帮忙 ^_^ Input 输入包括

HDU 1596 find the safest road (最短路)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6973    Accepted Submission(s): 2469 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

hdu 1596 find the safest road (最短路径)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6119    Accepted Submission(s): 2178 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间

hdu 1596 find the safest road

http://acm.hdu.edu.cn/showproblem.php?pid=1596 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 1001 5 using namespace std; 6 7 double g[maxn][maxn]; 8 int dis[maxn]; 9 bool vis[maxn]; 10 int n,a,b; 11 12 vo

杭电 1596 find the safest road (最短路)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的,只不过是把+改成了*,输入输出有些不一样而已. find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6985    Accepted Submission(s)

hdu 1596 find the safest road(dijkstra||floyd)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8192    Accepted Submission(s): 2901 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

hdu 1596 find the safest road (Dijksrta算法)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7536    Accepted Submission(s): 2664 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

HDU 1596 find the safest road &lt;SPFA算法的一个变形&gt;

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9377    Accepted Submission(s): 3313 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

hdu 1596 find the safest road(最长路)

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9285    Accepted Submission(s): 3287 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1