mmdbcn 发表于 2015-11-24 14:09:21

Currency Exchange(bellman-Ford)

  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

货币互相有转换的公式,问能不能通过转化,钱变得多一点。

不会做……看了网上的做法,才知道这个是bellman的模板题,其实题目就是想让我们找负环,在这里,负环指得是一个环,每走一次,钱都会增加,那么我们走无限次,再回去,一定是增加财富的。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
double d;
int leap,head;
struct Node
{
int u,v,next;
double r,c;
}node;
void add_edge(int u,int v,double r,double c,int tot)
{
node.u = u;node.v = v;
node.r = r;node.c = c;
node.next = head;
head = tot;
}
void relax(int u,int v,double r,double c)
{
if(d >= c)
d = max(d,(d - c)*r);
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int n,m,s;
double v;
while(scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF)
{
for(int i = 1;i <= n;i++)head = -1;
for(int i = 1;i <= m;i++)
{
int from,to;
double r,c;
scanf("%d%d%lf%lf",&from,&to,&r,&c);
add_edge(from,to,r,c,2*i - 1);
scanf("%lf%lf",&r,&c);
add_edge(to,from,r,c,2*i);
}
for(int i = 1;i <= n;i++)d = 0;
for(int i = 1;i <= n;i++)leap = 0;
d = v;
for(int i = 1;i < n;i++)
{
for(int j = 1;j <= m*2;j++)
relax(node.u,node.v,node.r,node.c);
}
bool flag = 0;
for(int i = 1;i <= m*2;i++)
{
int from = node.u;
int to = node.v;
if(d >= node.c)
{
if((d - node.c)*node.r > d)
{
flag = 1;break;
}
}
}
if(flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}
页: [1]
查看完整版本: Currency Exchange(bellman-Ford)