POJ 1860 Currency Exchange(BellmanFord最短路径的变形)

Currency Exchange

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 11843

 

Accepted: 3984

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

Source

Northeastern Europe 2001, Northern Subregion

 解题报告:已知有n种货币,编号为1~n,以及它们之间的兑换比例。假设你持有一种货币(S) 面值V;让你去交换其他货币,如果你能找到一个交换路径 使得你的钱一直在增加 则输出YES 否则 输出NO;构建图:设货币为点, 他们之间的交换为边; (现有货币-手续费) *汇率 为边的关系函数;题意即求 最长路径并判断图 是否存在 正权回路;这就是Bellman-Ford最短路径的变形,在注意数据的类型!

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int N = 110;
int n, m, s;
double v;
double dis[N];//注意类型
struct node
{
int a, b;
double r, c;
}e[2 * N];
int Bellman_Ford()
{
int i, j;
memset(dis, 0, sizeof(dis));//初始化
dis[s] = v;
for (i = 1; i < n; ++i)
{
int flag = 0;
for (j = 1; j <= 2 * m; ++j)
{
if (dis[e[j].b] < (dis[e[j].a] - e[j].c) * e[j].r)//进行松弛处理
{
dis[e[j].b] = (dis[e[j].a] - e[j].c) * e[j].r;
flag = 1;
}
}
if (!flag)
{
break;
}
}
for (i = 1; i <= 2 * m; ++i)//判断是否有正权回路
{
if (dis[e[i].b] < (dis[e[i].a] - e[i].c) * e[i].r)
{
return 1;
}
}
return 0;
}
int main()
{
int i;
//freopen("POJ 1860 Currency Exchange.txt", "r", stdin);
scanf("%d%d%d%lf", &n, &m, &s, &v);
for (i = 1; i <= m; ++i)
{
scanf("%d%d%lf%lf", &e[i].a, &e[i].b, &e[i].r, &e[i].c);
e[i + m].a = e[i].b;
e[i + m].b = e[i].a;
scanf("%lf%lf", &e[i + m].r, &e[i + m].c);//另一种的兑换
}
if (Bellman_Ford())
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}



原文地址:https://www.cnblogs.com/lidaojian/p/2378682.html