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算法是用于解决有边数限制的求解最短路问题. ...
随机推荐
- 模板与继承之艺术——奇特的递归模板模式(CRTP)
一.什么是CRTP 奇特的模板递归模式(Curiously Recurring Template Pattern)即将派生类本身作为模板参数传递给基类. template<typename T& ...
- java学习笔记_MIDI_GUI
import javax.sound.midi.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class M ...
- HTML5之拖放
- Draggable 标签 文件拖放 99年IE5开始,05后所有浏览器支持(除了opera) <li id=be draggable=true ondragstart="star ...
- 关于mysql group_concat 不能显示为空的列的其他信息
今天做项目遇到一个问题,百度好久都没找到问题所在 是酱紫的,一张表 关联的表 然后我用sql语句查询 point.pid,point.pname,GROUP_CONCAT(downsite.pname ...
- OpenCV3读取、写入和保存图像
需要说明的是在OpenCV3中已经将imread()和imwrite()函数转移到imgcodecs模块中,因此读写图像时,需要包含imgcodecs.hpp头文件,但是highgui.hpp头文件中 ...
- HTTP 错误 404.2 解决方案
HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面 详细错误:HTTP 错误 404.2 - Not Found ...
- Linux各发行版本 优缺点 简介
2008.01.21 13:43 Linux最早由Linus Benedict Torvalds在1991年开始编写.在这之前,RichardStallman创建了Free SoftwareFound ...
- java 中的equal和"=="
先看一段代码 String str1 = new String("str"); String str2 = new String("str"); System. ...
- 大批量DML操作应该注意什么?
问:大批量DML操作应该注意什么? 答:大批量DML操作可能会撑爆undo表空间,导致数据库挂起.因此我们应该设置一个合适的undo表空间,或对DML操作的分批提交.
- EXTJS 4.2 资料 控件之Window窗体相关属性的用法
最大化,最小化,是否显示关闭按钮 var win_CommonPicLibMultiple = Ext.create("Ext.window.Window", { title: & ...