Currency Exchange

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

题目大意:

    给定一种货币的本金。再给定一些货币之间转换的汇率和手续费。判断是否可以通过货币之间的转换来使本金变多。

解题思路:

    根据给定的货币转换建立有向图。

    使用Bellman-Ford算法对有向图进行N-1次松弛。 (N为顶点个数)

    若一个图不存在正权回路,则最多进行N-1次松弛,若存在正权回路,则可以再进行松弛。    

Code:

 /*************************************************************************
> File Name: poj1860.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月17日 星期五 17时08分07秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 2000
using namespace std;
int N,M,S,k;
double dis[MAXN];
double V;
struct edge
{
int begin,end;
double r,c;
}Edge[MAXN];
bool Bellman()
{
memset(dis,,sizeof(dis));
dis[S]=V;
bool flag=;
for (int i=;i<=N-;i++)
{
flag=;
for (int j=;j<=k;j++)
if (dis[Edge[j].end]<(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r)
{
dis[Edge[j].end]=(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r;
flag=;
}
if (!flag) break;
}
//再次判断是否会增加,增加则表示出现了正权回路
for (int j=;j<=k;j++)
if(dis[Edge[j].end]<(dis[Edge[j].begin]-Edge[j].c)*Edge[j].r)
return ;
return ;
}
int main()
{
while (scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF)
{
k=;
for (int i=;i<=M;i++)
{
int a,b;
double rab,rba,cab,cba;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
Edge[k].begin=a;
Edge[k].end=b;
Edge[k].r=rab,Edge[k].c=cab;
k++;
Edge[k].begin=b;
Edge[k].end=a;
Edge[k].r=rba,Edge[k].c=cba;
k++;
}
k--;
bool ok=Bellman();
if (ok) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

POJ1860——Currency Exchange(BellmanFord算法求最短路)的更多相关文章

  1. [poj1860] Currency Exchange (bellman-ford算法)

    题目链接:http://poj.org/problem?id=1860 题目大意:给你一些兑换方式,问你能否通过换钱来赚钱? 使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO ...

  2. bellman-ford算法求K短路O(n*m),以及判负环O(n*m)

    #include<iostream> #include<algorithm> #include<cstring> using namespace std; cons ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)

    1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...

  5. 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)

    Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...

  6. POJ1860 Currency Exchange —— spfa求正环

    题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  7. POJ1860 Currency Exchange(bellman-ford)

    链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...

  8. POJ1860 Currency Exchange【最短路-判断环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  9. Bellman-Ford算法 求有边数限制的最短路

    这个算法也是紧承我们之前讲过的关于图论的内容,我们在前面分析图的时候说过了对于不同的图论问题,我们会有不同的求解方法,那么这里我们讲到Bellman-Ford算法是用于解决有边数限制的求解最短路问题. ...

随机推荐

  1. Linux 锁

    问题: 1.假如对某个文件加了锁/lock,但是程序退出时没有关闭锁,如果想在另外一个程序中用这个文件,如何办? 2.

  2. Windows内存管理[转]

    本文主要内容:1.基本概念:物理内存.虚拟内存:物理地址.虚拟地址.逻辑地址:页目录,页表2.Windows内存管理3.CPU段式内存管理4.CPU页式内存管理 一.基本概念1. 两个内存概念物理内存 ...

  3. L002-oldboy-mysql-dba-lesson02

            L002-oldboy-mysql-dba-lesson02 [root@web01 ~]# mysql -uroot -ptestpassword mysql> use mys ...

  4. linux文件权限位SUID,SGID,sticky的设置理解

    SUID含义:文件的该位被设置为1,在该文件被执行时,该文件将以所有者的身份运行,也就是说无论谁来           执行这个文件,他都有文件所有者的特权,如果所有者是root的话,那么执行人就有超 ...

  5. C#定时器

    在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 S ...

  6. 使用XFire+Spring构建Web Service

    XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用. 相 对于Axis来说,目 ...

  7. 在SAE上同步djanogo的mysql数据库

    折腾了一个下午,终于搞掂了把djanogo应用的mysql数据库导入到SAE上了,归根到底麻烦的根源是SAE限制多多.下面简单记录一下过程以备日后参考使用. 首先还是修改settings.py,把数据 ...

  8. [译] ASP.NET 生命周期 – ASP.NET 应用生命周期(一)

    概述 ASP.NET 平台定义了两个非常重要的生命周期.第一个是 应用生命周期  (application life cycle),用来追踪应用从启动的那一刻到终止的那一刻.另一个就是 请求生命周期 ...

  9. 前端跨域之html5 XMLHttpRequest Level2

    前端代码 var xhr=new XMLHttpRequest(); xhr.open('POST','http://127.0.0.1:8081/ceshi',true); xhr.onreadys ...

  10. 域名的a记录转过来他的公网ip

    首先 客户要我把域名 和项目进行绑定客户需要提供万网或者新网的账户,登进去域名管理  选项域名管理的A记录定向到那个公网ip上 与服务器做绑定然后 在服务器的iis 上 加个主机头 输入主机头名称 也 ...