POJ1860——Currency Exchange(BellmanFord算法求最短路)
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算法求最短路)的更多相关文章
- [poj1860] Currency Exchange (bellman-ford算法)
题目链接:http://poj.org/problem?id=1860 题目大意:给你一些兑换方式,问你能否通过换钱来赚钱? 使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO ...
- bellman-ford算法求K短路O(n*m),以及判负环O(n*m)
#include<iostream> #include<algorithm> #include<cstring> using namespace std; cons ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- 51nod 1445 变色DNA ( Bellman-Ford算法求单源最短路径)
1445 变色DNA 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一只特别的狼,它在每个夜晚会进行变色,研究发现它可以变成N种颜色之一,将这些颜色标号为0,1 ...
- 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)
Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Bellman-Ford算法 求有边数限制的最短路
这个算法也是紧承我们之前讲过的关于图论的内容,我们在前面分析图的时候说过了对于不同的图论问题,我们会有不同的求解方法,那么这里我们讲到Bellman-Ford算法是用于解决有边数限制的求解最短路问题. ...
随机推荐
- day 0.
/* 嗯 就要结束了. OI生涯 2015.12-2016.11. 认识了很多人. 然后我这个学渣跟你们混在一起 感觉自卑至极啊. 好了 先不说这些伤心的话. Gryz小伙伴儿们NOIP RP++吧. ...
- 考虑virtual函数以外的其它选择
详情见<Effective C++>item35 1.使用non-virtual interface(NVI)手法,这是Template Method设计模式的一种特殊形式. 它以publ ...
- 开发问题记录——AE开发提示80040111错误
System.runtime.interpServices.ComException(0X80040111): 80040111 ClassFactory无法供应请求的类(异常来自HRESULT:0X ...
- WeX5是主要进行app开发吗?能开发微信App吗?
WeX5是一款html5开发工具,可以进行app开发,做出各种H5 App,同样也可以进行主要运行在PC的html5产品,. WeX5开发的应用,不仅可以在微信上运行,也可以直接手机浏览器运行,或者打 ...
- Get code int value for different encoding
http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.getencoding%28v=vs.110%29.aspx usin ...
- AngularJS(9)-表单
AngularJS 表单是输入控件的集合 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- phpcmsV9中表单向导在js调用里日期控件在IE下报Calendar未定义的解决办法
最近在phpcmsV9里用表单向导弄个的提交表单,但用了日期和时间类型时,用 <script language='javascript' src='{APP_PATH}index.php?m ...
- HashMap加入数据后,会自动根据首字母排序
1.Map<String, ArrayList<XX>> entityHashMap = new HashMap<>(); 然后增加一些数据,会发现根据String ...
- (转)《深入理解java虚拟机》学习笔记6——类加载机制
Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程. 在加载阶段,java虚拟机需要完成以下 ...
- gridView AspNetPager 翻页时 弹出窗体关闭报错
gridView AspNetPager 翻页后,你右击刷新或F5会发现弹出一个刷新页面. 这是因为默认翻页都是用dopostback方式回发的.因为这时的页面已经不是原来的页面.所以会弹出提示. 这 ...