【最短路-判断正权环 Floyd】Currency Exchange POJ - 1860
Currency Exchange POJ - 1860
题意:
这题和Arbitrage POJ - 2240一个意思,区别在于本题给定了起始货币种类及具体数量,并且在兑换其他货币前需要扣一笔手续费,以及是双向边。
思路:
和Arbitrage POJ - 2240一样的Bellman-Ford解法。
Floyd
int n, m, s;
double v;
double edges[maxn][maxn];
double com[maxn][maxn];
double d[maxn];
double temp[maxn];
bool solve() {
for (int i = 1; i <= n; i++) {
temp[i] = d[i];
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((d[i]-com[i][j]) * edges[i][j] > d[j]) {
d[j] = (d[i] - com[i][j]) * edges[i][j];
}
}
}
}
for (int i = 1; i <= n; i++) {
if (d[i] > temp[i]) return true;
}
return false;
}
int main()
{
// ios::sync_with_stdio(false);
/// int t; cin >> t; while (t--) {
cin >> n >> m >> s >> v;
memset(edges, INF, sizeof(edges));
memset(d, 0, sizeof(INF));
for (int i = 1; i <= m; i++) {
int a, b;
double ab_rate, ab_com, ba_rate, ba_com;
cin >> a >> b >> ab_rate >> ab_com >> ba_rate >> ba_com;
edges[a][b] = ab_rate;
com[a][b] = ab_com;
edges[b][a] = ba_rate;
com[b][a] = ba_com;
}
d[s] = v;
solve();
if (solve()) cout << "YES";
else cout << "NO";
return 0;
}