题目链接:http://poj.org/problem?id=1860
Time Limit: 1000MS Memory Limit: 30000K
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.
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.
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
Problem solving report:
Description: 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s金额数能否增加。
Problem solving: 货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的。
正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛。我们求解图的负权回路的时候利用的是边的松弛操作和Spfa的回路判断(一个点入队超过点数);求解图的正权回路的时候利用的是边的放大操作和Spfa的回路判断(一个点入队超过点数)。核心在于Spfa算法在出现回路的时候如果不加限定会导致无限的入队,这样的话,我们可以根据我们的入队次数来判断的图中是否存在环。
Accepted Code:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
bool vis[MAXN];
double dis[MAXN];
int f[MAXN], inq[MAXN], cnt;
struct edge {
int u, v;
double r, c;
edge(int u_ = 0, int v_ = 0, double r_ = 0, double c_ = 0) : u(u_), v(v_), r(r_), c(c_) {}
}e[MAXN << 1];
inline void Add(int u, int v, double r, double c) {
e[++cnt] = edge(f[u], v, r, c);
f[u] = cnt;
}
inline void init() {
cnt = 0;
memset(f, -1, sizeof(f));
memset(dis, 0, sizeof(dis));
memset(inq, 0, sizeof(inq));
memset(vis, false, sizeof(vis));
}
bool Spfa(int s, int n, double val) {
queue <int> Q;
Q.push(s);
dis[s] = val;
vis[s] = true;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
vis[u] = false;
for (int i = f[u]; ~i; i = e[i].u) {
int v = e[i].v;
double r = e[i].r, c = e[i].c;
if (dis[v] < (dis[u] - c) * r) {
dis[v] = (dis[u] - c) * r;
if (!vis[v]) {
Q.push(v);
vis[v] = true;
}
if (++inq[v] > n)
return true;
}
}
}
return false;
}
int main() {
init();
double val, r, c;
int n, m, s, u, v;
scanf("%d%d%d%lf", &n, &m, &s, &val);
for (int i = 0; i < m; i++) {
scanf("%d%d%lf%lf", &u, &v, &r, &c);
Add(u, v, r, c);
scanf("%lf%lf", &r, &c);
Add(v, u, r, c);
}
if (Spfa(s, n, val))
printf("YES\n");
else printf("NO\n");
return 0;
}