最短路变形题,相当于求最长路,Bellmanford算法轻松搞定。目测Dijkstra也可以解决,待会试一下。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define MAXN 105
using namespace std;
struct Edge
{
int from,to;
double weight;
void Set(int a,int b,double v)
{
from=a;
to=b;
weight=v;
}
};
int main()
{
int n,m;
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
int u,v,w,N=0;
Edge p[MAXN*MAXN];
for(int i=0; i<m; ++i)
{
scanf("%d%d%d",&u,&v,&w);
p[N++].Set(u,v,w/100.0);
p[N++].Set(v,u,w/100.0);
}
double dism[MAXN]= {0};
dism[1]=1;
for(int i=0; i<n-1; ++i)
for(int j=0; j<N; ++j)
{
int to=p[j].to,from=p[j].from;
double cost=p[j].weight;
dism[to]=max(dism[to],dism[from]*cost);
}
printf("%lf percent\n",dism[n]*100);
}
return 0;
}