POJ1860-最短路

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 35546   Accepted: 13629

Description

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, CAB, RBA and CBA - 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<=103. 
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102. 
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 104. 

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

题意:一个人要去换钱,先给出自己有多少钱,钱的类型是什么,问最后能不能靠换钱赚钱

换钱的时候有汇率和佣金,公式是(自己的钱的价值 - 佣金) * 汇率 = 换后的钱的价值

扫描二维码关注公众号,回复: 2285834 查看本文章

输入一坨数据   n  表示多少点 (多少种钱)  m  表示多少条路线 (换钱的地点)  s   v表示    初始点(自己钱的类型)和初始权值(自己钱的价值)

后面接的是   m  条路线(换钱点的换钱方式)

1 - > 2 第一种钱换成第二种钱  ,汇率是1.00 ,佣金是1.00     2 - > 1   第二种钱换第一种钱   汇率是  1.00   佣金是   1.00

这是第一组数据,前面两个实数是1换2的数据,后面两个实数是2换1 的数据

思路:最短路问题,这个是最长路。。。。。。因为可能换的钱会变少,相当于负值,用Bellman-Ford算法就可以了

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
	int u,v;
	double r,c;
}array[220];
double dis[220];
int n,m,s,k;
double v;
int Bellman_Ford()
{
	int i,j,flag;
	int a,b;
	memset(dis,0,sizeof(dis));       //最短路是INF   最长路就是0
	dis[s]=v;
	for (i=1;i<n;i++)
		{
			flag=0;
			for (j=1;j<k;j++)
				{
					a=array[j].u;b=array[j].v;
					if (dis[b]<(dis[a]-array[j].c)*array[j].r)    //最长的判断
						{
							dis[b]=(dis[a]-array[j].c)*array[j].r;
							flag=1;
						}
				}
			if (flag==0)       //无法继续松弛的情况后,跳出
				break;
		}
	for (i=1;i<k;i++)
		if (dis[array[i].v]<(dis[array[i].u]-array[i].c)*array[i].r)     //如果有赚钱的情况,返回1
			return 1;
	return 0;
}
int main()
{
	int i,a,b,ans;
	double ruv,cuv,rvu,cvu;
	k=1;
	scanf("%d%d%d%lf",&n,&m,&s,&v);
	for (i=1;i<=m;i++)
		{
			scanf("%d%d%lf%lf%lf%lf",&a,&b,&ruv,&cuv,&rvu,&cvu);
			array[k].u=a;
			array[k].v=b;
			array[k].r=ruv;
			array[k++].c=cuv;
			array[k].u=b;
			array[k].v=a;
			array[k].r=rvu;
			array[k++].c=cvu;
		}
	ans=Bellman_Ford();
	if (ans==1)
		cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/81109134