【最短路】poj2472 SPFA

来源:互联网 发布:qq三国奥义感悟的算法 编辑:程序博客网 时间:2024/04/29 10:08

细节更改 改为最长路 注意实数

翻译来自scy


Description
出发点为点1,目标点是点n,求一条路,该路从点1到点n安全性最大。


Input
输入有多组数据。
Each test case starts with two integers n and m (2 <= n <= 100 , 1 <= m <= n*(n-1)/2). n表示有n个点,m表示m条边。下来m行,每行三个数a,b和p(注意p是double)表示a和b之间存在一条安全性为p的边。(1 <= a, b <= n , a != b, 1 <= p <= 100)。
比如  1到2是60安全,2到3是40安全 ,那么1到3就是24安全 ,应为 60%×40%=24%
假设点1和点n一定联通的。
最后一组测试数据以0结束。


Output
梅泽测试数据输出“x percent”x为安全性,保留六位小数。


Sample Input
5 7
5 2 100
3 5 80
2 3 70
2 1 50
3 4 90
4 1 85
3 1 70
0

#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>#include<iostream>using namespace std;const int N=110;int n,m,len;int first[N];double d[N];bool vis[N];struct node{int x,y,next;double d;}a[N*N];void ins(int x,int y,double t){len++;t/=100;a[len].x=x;a[len].y=y;a[len].d=t;a[len].next=first[x];first[x]=len;}int main(){int i,j;while(1){scanf("%d",&n);if(!n) return 0;scanf("%d",&m);    len=0;    memset(first,0,sizeof(first));    for(i=1;i<=m;i++)    {    int x,y;    double t;    scanf("%d%d%lf",&x,&y,&t);    ins(x,y,t);    ins(y,x,t);    }d[1]=1.00;for(i=2;i<=n;i++) d[i]=0.00;memset(vis,0,sizeof(vis));queue<int> q;q.push(1);while(!q.empty()){int x=q.front();q.pop();vis[x]=1;for(i=first[x];i;i=a[i].next){int y=a[i].y;if(d[x]*a[i].d>d[y]){d[y]=d[x]*a[i].d;if(!vis[y]){vis[y]=1;q.push(y);}}}vis[x]=0;}printf("%lf percent\n",d[n]*100);}}


0 0