当前位置:   article > 正文

乘积最长路【poj】2472_求图中最大乘积路径

求图中最大乘积路径

题目链接
题意:求图的乘积最长路,迪杰斯特拉变形,初始化需要用零元更新

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1001;
double dis[N],g[N][N];
bool st[N];
int n,m;
void dijistra()
{
    for(int i=1;i<=n;i++)
    {
        int t=-1;
        for(int j=1;j<=n;j++)
            if(st[j]==0 && (t==-1 || dis[t]<dis[j]))
                t=j;
        st[t]=1;
        for(int j=1;j<=n;j++)
            dis[j]=max(dis[j],dis[t]*g[t][j]);
    }
}

int main()
{
    while(cin>>n&&n)
    {
        cin>>m;
        memset(dis,0,sizeof dis);
        dis[1]=1;
        memset(g,0,sizeof g);
        memset(st,0,sizeof st);
        while(m--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            g[a][b]=g[b][a]=max(g[a][b],c/100.0);
        }
        dijistra();
        printf("%.6lf percent\n",dis[n]*100.0);
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/52926
推荐阅读
相关标签
  

闽ICP备14008679号