poj 1860 Currency Exchange (Bellman_Ford)

点击打开链接

第一次写Bellman_Ford..

#include"stdio.h"
int n,m,start;
double money,rab,rba,cba,cab;
struct node
{
	int x,y;
	double r,c;
}map[210];
double d[210];
int Bellman_Ford()
{
	int i,j;
	for(i=0;i<n;i++)
		d[i]=0;
	d[start]=money;
	for(i=1;i<n;i++)
	{
		for(j=0;j<2*m;j++)
		{
			if(d[map[j].y]<(d[map[j].x]-map[j].c)*map[j].r)
				d[map[j].y]=(d[map[j].x]-map[j].c)*map[j].r;
		}
	}
	for(i=0;i<2*m;i++)
	{
		if(d[map[i].y]<(d[map[i].x]-map[i].c)*map[i].r)
			return 1;
	}
	return 0;
}
int main()
{
	int i,a,b;
	while(scanf("%d%d%d%lf",&n,&m,&start,&money)!=-1)
	{
		for(i=0;i<m;i++)
		{
			scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
			map[i].x=a;
			map[i].y=b;
			map[i].c=cab;
			map[i].r=rab;
			map[i+m].x=b;
			map[i+m].y=a;
			map[i+m].c=cba;
			map[i+m].r=rba;
		}
		if(Bellman_Ford())
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
		


你可能感兴趣的:(poj 1860 Currency Exchange (Bellman_Ford))