Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 18881 | Accepted: 6733 |
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.
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.
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
这个题已经困扰了我四天了 以前一直没怎么刷过图的题 刚一上手比较陌生 先研究了好几天最短路才开始刷题的 结果一上来被卡了四天 sad sad sad
题目大意 换货币 输入N-货币种类 M-兑换方式的种数 S-Nick手中的货币类型 V-Nick手中的货币面额
输入M行 每行a,b,表示货币类型rab,cab,rba,cba,a->b的兑换率,手续费。b->a的兑换率,手续费。
最后计算能不能让自己手中的货币升值。
a->b Vb=(Va-cab)*rab;
用贝尔曼福德,只要有负环就是YES,没有就是NO
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define Cmp(a,b) strcmp(a,b)
#define Copy(a,b) strcpy(a,b);
#define pc(a) printf("Case %d:",a)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMF(a) memset(a,false,sizeof(a))
#define w1 while(1)
#define w(a) while(a--)
#define INF 4294967295
#define PI 3.14159265359
const int dir4[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
const int dir8[8][2]= {{-1,0},{1,0},{0,-1},{0,1},{-1,1},{1,-1},{-1,-1},{1,1}};
/***********************************************/
struct node
{
int u,v;
double r,c;
} Map[500];
int BF(int n,int k,int x,double value)
{
double d[501];
MEM(d);
d[x]=value;
for(int i=1; i<n; i++)
{
for(int j=0; j<k; j++)
{
int a=Map[j].u;
int b=Map[j].v;
double r=Map[j].r;
double c=Map[j].c;
if(d[b]<(d[a]-c)*r)
{
d[b]=(d[a]-c)*r;
}
}
}
for(int j=0; j<k; j++)//判断是否有负环 有的话直接可以无限换 就可以进行升值操作了
{
int a=Map[j].u;
int b=Map[j].v;
double r=Map[j].r;
double c=Map[j].c;
if(d[b]<(d[a]-c)*r)
return 1;
}
return 0;
}
int main()
{
int n,m,x;
double value;
cin>>n>>m>>x>>value;
int a,b;
double ra,rb,ca,cb;
int k=0;
w(m)
{
cin>>a>>b>>ra>>ca>>rb>>cb;
Map[k].u=a;
Map[k].v=b;
Map[k].r=ra;
Map[k++].c=ca;
Map[k].u=b;
Map[k].v=a;
Map[k].r=rb;
Map[k++].c=cb;
}
int ans=BF(n,k,x,value);
if(ans)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}