POJ - 1860 currency exchange(bellman)

题目大意:有几个货币兑换点进行货币兑换,需要支付一部分佣金,例如美元换卢布汇率是 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值