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
FY
我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布。 你肯定知道在我们的城市里你可以处理不同的货币。让每一种货币都用唯一的一个小于N的整数表示。然后每个交换点,可以用6个整数表描述:整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。 nick有一些钱在货币S,他希望能通过一些操作(在不同的兑换点兑换),增加他的资本。当然,他想在最后手中的钱仍然是S。帮他解答这个难题,看他能不能完成这个愿望。
Input
第一行四个数,N,表示货币的总数;M,兑换点的数目;S,nick手上的钱的类型;V,nick手上的钱的数目;1<=S<=N<=100, 1<=M<=100, V 是一个实数 0<=V<=103. 接下来M行,每行六个数,整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金(0<=佣金<=102,10-2<=汇率<=102) 4.
Output
如果nick能够实现他的愿望,则输出YES,否则输出NO。
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
题解:
刚开始在看着道题的时候,看到了一个玄而又玄的解题方法:
直接用三层for循环进行查找,但是这个算法又不像是Floy算法~~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=106;
int N,M,S;
double V;
double mp1[maxn][maxn];
double mp2[maxn][maxn];
double mp[maxn];
double d[maxn];
double floy()
{
for(int i=1;i<=N;++i)
d[i]=mp[i];
for(int k=1;k<=N;++k)
for(int i=1;i<=N;++i)
for(int j=1;j<=N;++j)
{
if((mp[i]-mp2[i][j])*mp1[i][j]>mp[j])
mp[j]=(mp[i]-mp2[i][j])*mp1[i][j];
}
for(int i=1;i<=N;++i)
{
if(mp[i]>d[i])
return 1;
}
return 0;
}
int main()
{
cin>>N>>M>>S>>V;
while(M--)
{
int a,b;
double x1,x2,y1,y2;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&x1,&x2,&y1,&y2);
mp1[a][b]=x1;
mp1[b][a]=y1;
mp2[a][b]=x2;
mp2[b][a]=y2;
//mp1代表汇率,mp2代表佣金
}
mp[S]=V;
floy();
if(floy())printf("YES\n");
else printf("NO\n");
return 0;
}
本弱弱对于main函数里面的两个floy函数的理解是,第一个是找到循环后的钱数,然后作为第二层循环的比较。
还是说一下bellman-Ford算法吧
如果对于这个算法还不太了解,可以参考一下这篇博客
在刚接触到Bellmen-Ford算法的时候,是用来解决负边的问题(这道题本来是用Dijkstrar算法做的,但是Dijkstar算法不支持边权为负的情况。)并且一般的初始化是最大值,然后寻找的是最短路,不过这道题刚好跟常规的松弛方法相反,需要初始化为0,然后寻找最大值。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=205;
int N,M,S,cnt;
double V;
double d[maxn];
struct xiao
{
int a;
int b;
double hv;//汇率
double yj;//佣金
}mp[maxn];
bool Bellmen()
{
memset(d,0,sizeof(d));
d[S]=V;//这里和Bellmen刚好相反
for(int i=1;i<=N-1;++i)
{
bool flag=false;
for(int j=0;j<cnt;++j)
{
if(d[mp[j].b]<(d[mp[j].a]-mp[j].yj)*mp[j].hv)
{
d[mp[j].b]=(d[mp[j].a]-mp[j].yj)*mp[j].hv;
flag=true;
}//这里来循环找最大值
}
if(!flag) break;
}
bool flag=false;
for(int i=0;i<cnt;++i)
{
if(d[mp[i].b]<(d[mp[i].a]-mp[i].yj)*mp[i].hv)
{
flag=true;
break;
}
}
return flag;
}
int main()
{
scanf("%d%d%d%lf",&N,&M,&S,&V);
while(M--)
{
int a,b;
double hl1,yj1,hl2,yj2;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&hl1,&yj1,&hl2,&yj2);
mp[cnt].a=a;
mp[cnt].b=b;
mp[cnt].hv=hl1;
mp[cnt++].yj=yj1;
mp[cnt].a=b;
mp[cnt].b=a;
mp[cnt].hv=hl2;
mp[cnt++].yj=yj2;
}
if(Bellmen())
printf("YES\n");
else
printf("NO\n");
return 0;
}