Deformation of the shortest path.
The meaning of the question is to give you the probability that the road will not be caught. It is required to find the maximum probability of not being caught when the destination is reached.
Set the initial DIS [] to 1. The remaining values are 0. Find the maximum value.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;int n,m;struct lx{ int v; double p;};vector<lx> g[101];void SPFA(){ double dis[101]; bool vis[101]; for(int i=1;i<=n;i++) dis[i]=0,vis[i]=0; dis[1]=1.0,vis[1]=1; queue<int>q; q.push(1); while(!q.empty()) { int u=q.front();q.pop(); vis[u]=0; for(int j=0;j<g[u].size();j++) { int v=g[u][j].v; double p=g[u][j].p; if(dis[v]<dis[u]*p) { dis[v]=dis[u]*p; if(!vis[v]) { vis[v]=1; q.push(v); } } } } printf("%f percent\n",dis[n]*100);}int main(){ while(scanf("%d",&n),n) { scanf("%d",&m); for(int i=0;i<=n;i++) g[i].clear(); int u,v; double p; while(m--) { scanf("%d%d%lf",&u,&v,&p); lx now; now.p=p/100.0; now.v=v; g[u].push_back(now); now.v=u; g[v].push_back(now); } SPFA(); }}
Poj 2472 106 miles to Chicago