Currency Exchange POJ - 1860
我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的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。
Examples
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
Hint
题意:
题解:
直接套BellmanFord判断负环的版子, 改一下判断条件即可
经验小结:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1<<30;
const LL maxn = 110;
int N, M, S;
double V;
struct Edge{
double r, c;
int u, v;
Edge(int uu, int vv, double rr, double cc){u = uu, v = vv, r = rr, c = cc;}
};
vector<Edge> es;
double d[maxn]; //到达该点可获得的最大金额
bool BellmanFord(){
ms(d, 0);
d[S] = V;
for(int i = 1; i < N; ++i){
bool flag = false;
for(int j = 0; j < es.size(); ++j){
if((d[es[j].u]-es[j].c)*es[j].r > d[es[j].v]){
d[es[j].v] = (d[es[j].u]-es[j].c)*es[j].r ;
flag = true;
}
}
if(!flag) break;
}
for(int j = 0; j < es.size(); ++j)
if((d[es[j].u]-es[j].c)*es[j].r > d[es[j].v])
return true;
return false;
}
int main()
{
int a, b;
double Rab, Rba, Cab, Cba;
cin >> N >> M >> S >> V;
while(M--){
cin >> a >> b >> Rab >> Cab >> Rba >> Cba;
es.push_back(Edge(a, b, Rab, Cab));
es.push_back(Edge(b, a, Rba, Cba));
}
if(BellmanFord()) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}