hdu 5137 ( How Many Maos Does the Guanxi Worth )

本文介绍了一种使用 Dijkstra 算法求解从起点到终点的安全系数最大值的问题,通过实例演示了如何建立图并运行算法找到路径中安全系数的最大乘积。

题意:

给出矩阵,是i->j的安全系数,询问从s->t的安全系数最高是多少, 系数是满足*法

思路:

建立dijkstra跑最长路

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>

using namespace std;
const int maxn=1005;

double mp[maxn][maxn];

double ans[maxn];

struct node{
    int u;
    double d;
    node( int _u,double _d)
    {
        u=_u,d=_d;
    }
    bool friend operator <(node x, node y)
    {
        return x.d<y.d;
    }
};
double dis[maxn];
int n,m;
int vis[maxn];
void dijk(int s,int t)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=0;
        dis[i]=0;
    }
    priority_queue <node >q;
    q.push(node (s,1));
    dis[s]=1;
    while(!q.empty())
    {
        node tp=q.top();
        int u=tp.u;
        q.pop();
        if(t==u) return ;
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&dis[i]<dis[u]*mp[u][i])
            {
                q.push(node (i,dis[u]*mp[u][i]));
                dis[i]=dis[u]*mp[u][i];
            }
        }
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                scanf("%lf",&mp[i][j]);
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int s,t;
            scanf("%d%d",&s,&t);
            dijk(s,t);
            if(dis[t]<1e-8)
                printf("What a pity!\n");
            else printf("%.3lf\n",dis[t]);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值