题目大意:有几个货币兑换点进行货币兑换,需要支付一部分佣金,例如美元换卢布汇率是 29.75,手续费是 0.39, 那么 100 美元能够兑换 (100 - 0.39) * 29.75 = 2963.3975 卢布。第一行四个数,N货币总数,M兑换点数,S本金类型,V本金;接下来M行,每行六个数,整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。问通过兑换能否使得资产增加
解题思路:思路很简单,就是判断是否存在一个能够无限增大的环,利用 bellman 的思想,bellman 是判断是否存在一个无限缩小的环,即负环,改一下判断条件。注意实数,习惯性的开 int 数组,调了好久
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<string>
#include<queue>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int INF = 0x3f3f3f3f;
const int NINF = -INF -1;
const int MAXN = 200+5;
using namespace std;
int n, m, s, tot;
double v;
struct Node {
int a, b;
double r, c;
}node[MAXN];
double dis[MAXN];
bool bellman() {
dis[s] = v;
bool flag;
for (int i = 1; i < n; i++) {
flag = false;
for (int j = 0; j < tot; j++)
if (dis[node[j].b] < (dis[node[j].a]-node[j].c)*node[j].r) {
dis[node[j].b] = (dis[node[j].a]-node[j].c)*node[j].r;
flag = true;
}
if (!flag) break;
}
for (int i = 0; i < tot; i++)
if (dis[node[i].b] < (dis[node[i].a]-node[i].c)*node[i].r)
return true;
return false;
}
int main() {
while (scanf("%d%d%d%lf", &n, &m, &s, &v) != EOF) {
memset(dis, 0, sizeof(dis));
tot = 0;
for (int i = 0; i < m; i++) {
int a, b;
double rab, cab, rba, cba;
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
node[tot].a = a;
node[tot].b = b;
node[tot].r = rab;
node[tot++].c = cab;
node[tot].a = b;
node[tot].b = a;
node[tot].r = rba;
node[tot++].c = cba;
}
if (bellman()) printf("YES\n");
else printf("NO\n");
}
return 0;
}