题目链接:
https://cn.vjudge.net/problem/POJ-1860
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.
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
/*
题意描述
输入货币的种数n,货币兑换点数m,某人有货币种类s,价值hm
m个兑换点的规则分别是a兑换b,汇率是rab,费用是cab,b兑换a,汇率是rba,费用是cba,计算规则是(va-cab)*rab
问能否通过这m个兑换点使他的拥有s这种货币的钱数增加 解题思路
不管怎么兑换,关键是最后还要换回s这种钱,那么必须至少存在一个环,使得最后还能换回s,但是又要求增加钱数,那么这个换必须是正
环。所以问题变成了如何判断图中存在正环。使用Bellman_Ford算法判断正环即可。 样例中第二条边是1.10 不是1.00,看样例看了半天没看懂
注意函数调用时数据类型的变换,包括输入和函数传递参数时的数据类型
*/
#include<cstdio>
#include<cstring>
const int maxn = ; int u[maxn], v[maxn];
double ruv[maxn], cuv[maxn], dis[maxn];
int n, m, en;//边数
bool Bellman_Ford(int s, double hm); int main()
{
int a, b, s;
double hm, rab, cab, rba, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &hm) != EOF) {
en = ;
for(int i = ; i <= m; i++) {
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
u[en] = a; v[en] = b;
ruv[en] = rab;
cuv[en++] = cab; u[en] = b; v[en] = a;
ruv[en] = rba;
cuv[en++] = cba;
} if(Bellman_Ford(s, hm))
puts("YES");
else
puts("NO");
}
return ;
} bool Bellman_Ford(int s, double hm) {
memset(dis, , sizeof(dis));
dis[s] = hm; for(int i = ; i <= n; i++) {
for(int j = ; j < en; j++) {
if(dis[v[j]] < (dis[u[j]] - cuv[j]) * ruv[j]) {
dis[v[j]] = (dis[u[j]] - cuv[j]) * ruv[j];
if(i == n)
return ;//存在正环
}
}
}
return ;
}
使用结构体封装一下Bellman-Ford算法,再使用队列和邻接表优化一下,代码如下:
使用的时候注意数据类型的使用和结点数全部减1。
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std; const int maxn = ; struct Edge {
int from, to;
double rait, com;
Edge(int u, int v, double r, double c): from(u), to(v), rait(r), com(c) { }
}; struct Bellman_Ford {
int n, m, s;
double hm;
vector<Edge> edges;
vector<int> G[maxn];
double d[maxn];
bool inq[maxn];
int cnt[maxn]; void init(int n) {
this->n = n;
for(int i = ; i < n; i ++) {
G[i].clear();
}
edges.clear();
} void AddEdge(int from, int to, double rait, double com){
edges.push_back(Edge(from, to, rait, com));
m = edges.size();
G[from].push_back(m - );
} bool bellman_ford (int s, double hm) {
this->s = s;
this->hm = hm;
memset(d, , sizeof(d));
d[s] = hm; memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt)); queue<int> q;
q.push(s);
inq[s] = ; while(!q.empty()) {
int u = q.front();
q.pop(); inq[u] = ;
for(int i = ; i < G[u].size(); i++) {
Edge e = edges[G[u][i]];
if(d[e.to] < (d[u] - e.com) * e.rait){
d[e.to] = (d[u] - e.com) * e.rait;
if(!inq[e.to]) {
q.push(e.to);
inq[e.to] = ; cnt[e.to]++;
if(cnt[e.to] > n)
return ;
}
}
}
}
return ;
}
}; struct Bellman_Ford solve;
int main()
{
int s, a, b, n, m;
double hm, rab, cab, rba, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &hm) != EOF) {
solve.init(n);
for(int i = ; i <= m; i++) {
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
a--;
b--;
solve.AddEdge(a, b, rab, cab);
solve.AddEdge(b, a, rba, cba);
}
int ans = solve.bellman_ford(s-,hm); if(ans)
puts("YES");
else
puts("NO");
}
return ;
}