【最短路】poj2472 SPFA

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

翻译来自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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值