题目连接:http://poj.org/problem?id=1860

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

题目大意:有若干种货币,若干个兑换点,每个兑换点可以把一种货币兑换为另一种货币(可A->B,也可B->A),但是兑换有佣金,假设把A变为B,汇率为r,佣金为c,则B=(A-c)*r。给出这些兑换点的信息 以及 初始的钱的种类和数量,求是否可能进过若干次兑换使钱(最后必须是最开始的币种)变多;解题思路:转化为图,货币为节点,兑换点为边,则构成一个无向图,而问题就转化成了求次无向图是否存在正环(因为最后要化成开始的币种,而不是价值变多即可,所以是求正环)用Bellman——fold算法的思想,可以无限松弛即为正环,就可以解决了(原算法为求负环,只需把初始化的状态和松弛条件改一下即可)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>

using namespace std;

struct Edge
{
    int from,to;
    double r,c;
    Edge(int u,int v,double r,double c):from(u),to(v),r(r),c(c) {}
};

vector<];
vector<Edge> edges;
]= {};
]= {};
];
int n;

bool bellman_fold(int s,double value)
{
    queue<int> Q;
    memset(d,,sizeof(d));
    Q.push(s);
    d[s]=value;
    inq[s]=;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=;
        ; i<G[u].size(); i++)
        {
            int now=G[u][i];
            Edge & e=edges[now];
            &&d[e.to]<(d[u]-e.c)*e.r)
            {
                d[e.to]=(d[u]-e.c)*e.r;
                if(!inq[e.to])
                {
                    Q.push(e.to);
                    inq[e.to]=;
                    if(++cnt[e.to]>n)
                        ;
                }
            }
        }
    }
    ;
}

int main()
{
    int m,no;
    ;
    double sum;
    cin>>n>>m>>no>>sum;
    while(m--)
    {
        int no1,no2;
        double rab,cab,rba,cba;
        scanf("%d%d%lf%lf%lf%lf",&no1,&no2,&rab,&cab,&rba,&cba);
        edges.push_back(Edge(no1,no2,rab,cab));
        G[no1].push_back(x);
        x++;
        edges.push_back(Edge(no2,no1,rba,cba));
        G[no2].push_back(x);
        x++;
    }
    bool flag = bellman_fold(no,sum);
    if(flag)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
}

poj1860(Bellman—fold)的更多相关文章

  1. POJ1860(Currency Exchange)

    题意: 给出一张各种货币交换的网络,问在网络中交换原有的货币,问货币能否增值? 解析: 判断是否存在正环即可  用spfa  负环和正环的判定方法一样  如果一个点的进队次数超过n次 则存在环 代码如 ...

  2. [笔记]LibSVM源码剖析(java版)

    之前学习了SVM的原理(见http://www.cnblogs.com/bentuwuying/p/6444249.html),以及SMO算法的理论基础(见http://www.cnblogs.com ...

  3. LibSVM源码剖析(java版)

    之前学习了SVM的原理(见http://www.cnblogs.com/bentuwuying/p/6444249.html),以及SMO算法的理论基础(见http://www.cnblogs.com ...

  4. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  5. LibLinear(SVM包)使用说明之(一)README

    转自:http://blog.csdn.net/zouxy09/article/details/10947323/ LibLinear(SVM包)使用说明之(一)README zouxy09@qq.c ...

  6. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  7. 从零开始学ios开发(十八):Storyboards(下)

    这篇我们完成Storyboards的最后一个例子,之前的例子中没有view之间的切换,这篇加上这个功能,使Storyboards的功能完整呈现.在Storyboards中负责view切换的东西叫做“s ...

  8. linux —— shell 编程(文本处理)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...

  9. erlang程序优化点的总结(持续更新)

    转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多 ...

随机推荐

  1. 【bzoj2809】[Apio2012]dispatching 贪心+可并堆

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  2. hdu 3648 Median Filter (树状数组)

    Median Filter Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. 高性能服务器开发之C++定时器

    高性能服务器开发之C++定时器 来源: https://www.cnblogs.com/junye/p/5836552.html 写这篇文章前搜了下网上类似的文章,有很多,所以笔者的这篇文章就不对定时 ...

  4. 【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会

    01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\):求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最 ...

  5. BZOJ3243 [Noi2013]向量内积 【乱搞】

    题目链接 BZOJ3243 题解 模数只有\(2\)或\(3\),可以大力讨论 如果模数为\(2\),乘积结果只有\(1\)或\(0\) 如果一个向量和前面所有向量乘积都为\(1\),那么其和前面向量 ...

  6. mobx基本概念

    mobx是一个简单可扩展的状态管理库,主要用来管理状态之间的依赖关系,可以使用在任何状态管理的场景,并不仅限于react. 结合mobx-react可以用在react中,结合mobx-vue可以用在v ...

  7. [hdu 2102]bfs+注意INF

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的……把INF改成I ...

  8. matlab求最大公约数和最小公倍数

    最大公约数:(函数) function n = zuidagongyueshu(a,b) if(a>b) tem = a; b = a; a = tmp; end for i=1:a c = r ...

  9. canvas压缩图片变模糊问题

    canvas 画图图片变模糊问题 问题描述 在使用 canvas 对图片进行编辑导出图片之后发现图片和原图相比变得模糊了 canvas 画图线条变粗 问题产生原因 该问题在 PC 下面并不会产生,原因 ...

  10. oracle12c创建用户等问题

    一:前言 这几天我重新装了下电脑,然后自己有试着去装了下oracle11g,结果还是失败了然后我自己又去下载了最新的oracle12c,oracle12c中有两个用户sys和system,scott已 ...