poj1860Currency Exchange

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.


用bellman-ford不断进行松弛,当松弛V次时(顶点)说明图中有正权回路。

#include<iostream>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string>
using namespace std;
#define N 1000+5
#define LL long long int
#define pow(a) ((a)*(a))
#define mem(arr,a) memset(arr,a,sizeof(arr))
struct edge{
    int from, to;
    double rate, tax;
};
int n, m, s;
double sum;
edge es[N];
int E = 0;
double d[N];
bool bellman(int s){
    mem(d, 0);
    d[s] = sum;
    int cnt = 0;
    while (1){
        int v = -1;
        cnt++;
        for (int i = 0; i < E; i++){
            if (((d[es[i].from] - es[i].tax)>=0)&&d[es[i].to] < (d[es[i].from] - es[i].tax)*es[i].rate){
                d[es[i].to] = (d[es[i].from] - es[i].tax)*es[i].rate;
                v = 0;
            }
        }
        if (v)return true;
        if (cnt == n)return false;
    }
}
int main(){
    cin >> n >> m >> s >> sum;

    for (int i = 0; i < m; i++){
        cin >> es[E].from >> es[E].to >> es[E].rate >> es[E].tax;
        E++;
        es[E].from = es[E - 1].to;
        es[E].to = es[E - 1].from;
        cin >> es[E].rate >> es[E].tax;
        E++;
    }
    if (!bellman(s))cout << "YES" << endl;
    else cout << "NO" << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值