Currency Exchange POJ - 1860 SPFA最短路

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR. 
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, C AB, R BA and C BA - exchange rates and commissions when exchanging A to B and B to A respectively. 
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

Input

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10 3. 
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2. 
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10 4. 

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

用SPFA扫一遍如果有正环  就说明钱会增加

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm> 
#include<queue>
#include<cstdio>
#define clr(a,b) memset(a,b,sizeof(a));
using namespace std;
int n,m,s,index[106],visit[106];
double v,dis[106];

struct node
{
	int flag;
	double r,c;//rate commission
}map[106][106];//邻接矩阵 
//SPFA 搜的是节点 所以入队点而不是结构体 差点想成广搜 

int spfa()
{
	clr(visit,0)
	clr(index,0)
	clr(dis,0)
	dis[s]=v;
	visit[s]=1;//是否在队列中 
	index[s]=1;//入队列的次数 
	queue<int> q;
	q.push(s);
	int in;
	while(!q.empty())
	{
		in=q.front();
		q.pop();
		visit[in]=0;
		for(int i=1;i<=n;i++)
		{
			if(map[in][i].flag)
			{
				if(dis[i]<((dis[in]-map[in][i].c)*map[in][i].r)) //判断正环   原钱减去佣金乘上汇率如果大于原钱就更新 
				{
					dis[i]=((dis[in]-map[in][i].c)*map[in][i].r);
					if(!visit[i])
					{
						visit[i]=1;
						q.push(i);
						index[i]++;
						if(index[i]>n) //此点入队次数大于n说明 出现了环 因为松驰了n次还能松驰就是环的标志 
						{
							return 1;
						} 
					}
				}
			}
		}
		
	}
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
	while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=-1)
	{
	clr(map,0)
	for(int i=1;i<=m;i++)
	{
		int s,e;
		double r1,c1,r2,c2;
		cin>>s>>e>>r1>>c1>>r2>>c2;
		map[s][e].flag=1;
		map[s][e].r=r1;
		map[s][e].c=c1;
		map[e][s].flag=1;
		map[e][s].r=r2;
		map[e][s].c=c2;		
	}
	int ans=spfa();
	if(ans) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41544329/article/details/82628038