poj 1860 Currency Exchange (Bellman- Ford)

Currency Exchange

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 24432 Accepted: 8900

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 R
AB, 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

Source

Northeastern Europe 2001, Northern Subregion 题意:有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 – 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加 Bellman-Ford 算法的反向应用

Bellman-Ford算法描述:

  1,.初始化:将除源点外的所有顶点的最短距离估计值 d[v] ←+∞, d[s] ←0;

  2.迭代求解:反复对边集E中的每条边进行松弛操作,使得顶点集V中的每个顶点v的最短距离估计值逐步逼近其最短距离;(运行|v|-1次)

  3.检验负权回路:判断边集E中的每一条边的两个端点是否收敛。如果存在未收敛的顶点,则算法返回false,表明问题无解;否则算法返回true,并且从源点可达的顶点v的最短距离保存在 d[v]中。

  描述性证明:

  首先指出,图的任意一条最短路径既不能包含负权回路,也不会包含正权回路,因此它最多包含|v|-1条边。

  其次,从源点s可达的所有顶点如果 存在最短路径,则这些最短路径构成一个以s为根的最短路径树。Bellman-Ford算法的迭代松弛操作,实际上就是按顶点距离s的层次,逐层生成这棵最短路径树的过程。

与Dijkstra算法不同的是,在Bellman-Ford算法中,边的权值可以为负数。

      设想从我们可以从图中找到一个环路(即从v出发,经过若干个点之后又回到v)且这个环路中所有边的权值之和为负。那么通过这个环路,环路中任意两点的最短路径就可以无穷小下去。如果不处理这个负环路,程序就会永远运行下去。 而Bellman-Ford算法具有分辨这种负环路的能力

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,s,all;
double v;
double dis[1100];
struct node
{
	int a;
	int b;
	double r;
	double c;
}t[1100];
bool bellman()
{
	memset(dis,0,sizeof(dis));
	dis[s]=v;
	bool flag;
	for(int i=1;i<=n-1;i++)
	{
		flag=false;
		for(int j=0;j<all;j++)
			if(dis[t[j].b]<(dis[t[j].a]-t[j].c)*t[j].r)
			{
				dis[t[j].b]=(dis[t[j].a]-t[j].c)*t[j].r;
		    	flag=true;
			}
		if(!flag)
		break;
	}
	for(int k=0;k<all;k++)
		if(dis[t[k].b]<(dis[t[k].a]-t[k].c)*t[k].r)
		return true;
		return false;//说明存在负回路
}
int main()
{
	int a,b;
	double ab,ac,ca,cb;
	while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
	{
		all=0;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%lf%lf%lf%lf",&a,&b,&ab,&ac,&ca,&cb);
			t[all].a=a;
			t[all].b=b;
			t[all].r=ab;
			t[all++].c=ac;
			t[all].a=b;
			t[all].b=a;
			t[all].r=ca;
			t[all++].c=cb;
		}
		if(bellman())
		printf("YES\n");
		else
		printf("NO\n");
	}
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/yao1373446012/article/details/50752638
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞