Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 21046 | Accepted: 7539 |
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.
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.
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-货币种类 M-兑换方式的种数 S-Nick手中的货币类型 V-Nick手中的货币面额
输入M行 每行a,b,表示货币类型rab,cab,rba,cba,a->b的兑换率,手续费。b->a的兑换率,手续费。
最后计算能不能让自己手中的货币升值。
a->b Vb=(Va-cab)*rab;
真是没想到这个题是判负环的题,一开始我用bfs暴力搜索到每种货币的最短路,然后再把换完的货币总钱数最短路搜回到原始的那一种货币,然后与输入的钱数相比看看有没有增加,增加就是YES,否则就是NO,然后就一直WA,后来看了看别人的题解,发现他们都是判负环,如果有就是YES,否则NO,实在不明白,难道兑换货币手续费还倒贴???还是我理解错题意了?????
最后用spfa判负环方法AC:
#include <stdio.h>
#include <string.h>
#include <queue>
#define eps 1e-5
using namespace std;
struct node
{
int b;
double hl;
double sxf;
int next;
} ls[2010];
int head[110];
int num;
const int inf = -1;
void creat(int a,int b,double &chl,double &csxf)
{
ls[num].b = b;
ls[num].hl = chl;
ls[num].sxf = csxf;
ls[num].next = head[a];
head[a] = num++;
}
bool bfs(int n,int s,double &v)
{
int tj[110];
memset(tj,0,sizeof(tj));
int t,f;
double dis[110];
queue <int> q;
while(!q.empty())
q.pop();
for(int i = 0;i <= n;i++)
dis[i] = 0;
dis[s] = v;
bool vis[110];
memset(vis,false,sizeof(vis));
q.push(s);
vis[s] = true;
while(!q.empty())
{
// if(dis[s] > v)
// return true;
t = q.front();
q.pop();
for(int i = head[t];~i;i = ls[i].next)
{
f = ls[i].b;
if(dis[f] < (dis[t] - ls[i].sxf) * ls[i].hl)
{
dis[f] = (dis[t] - ls[i].sxf) * ls[i].hl;
if(!vis[f])
{
q.push(f);
vis[f] = true;
}
tj[f]++;
if(tj[f] > n)
return true;
}
}
vis[t] = false;
}
return false;
}
int main()
{
int n,m,s,a,b;
double hl1,hl2,sxf1,sxf2,v;
while(~scanf("%d%d%d%lf",&n,&m,&s,&v))
{
num = 0;
memset(head,-1,sizeof(head));
for(int i = 0; i < m; i++)
{
scanf("%d%d",&a,&b);
scanf("%lf%lf%lf%lf",&hl1,&sxf1,&hl2,&sxf2);
creat(a,b,hl1,sxf1);
creat(b,a,hl2,sxf2);
}
if(bfs(n,s,v))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
下面附上RE大神的BF算法一篇:(判负环模板,头文件模板)
#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;
}