POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860
Description
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
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
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 题目大意:有若干种货币,部分可以互相兑换,兑换时满足题目所给公式。现告诉你有几种货币,几种兑换方式,以及Nick所拥有的货币种类及其金额,问你能否通过若干次兑换后,兑回当前货币且金额增加,兑换过程中不能出现负值
解题思路:Bellman_Ford的变种,只需要将判负环的条件改为判正环即可,在进行松弛操作时,由于不能出现负值,将初始权值赋为0
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<map> using namespace std; typedef struct Edge{
int beg, end;
double r, c;
}Edge; int n, m, s, edges;
double v, dis[];
Edge edge[]; void setedge( int beg, int end, double r, double c ){
edges++;
edge[edges].beg = beg;
edge[edges].end = end;
edge[edges].r = r;
edge[edges].c = c;
} bool relax( int beg, int end, double r, double c ){
if( ( dis[beg] - c ) * r > dis[end] ){
dis[end] = ( dis[beg] - c ) * r;
return true;
}
return false;
} bool Bellman_Ford(){
for( int t = ; t < n; t++ ){
for( int i = ; i <= edges; i++ ){
relax( edge[i].beg, edge[i].end, edge[i].r, edge[i].c );
}
} for( int i = ; i <= edges; i++ ){
if( relax( edge[i].beg, edge[i].end, edge[i].r, edge[i].c ) )
return false;
} return true;
} int main(){
ios::sync_with_stdio( false ); while( cin >> n >> m >> s >> v ){
edges = ;
int beg, end;
double r1, c1, r2, c2;
while( m-- ){
cin >> beg >> end >> r1 >> c1 >> r2 >> c2;
setedge( beg, end, r1, c1 );
setedge( end, beg, r2, c2 );
}
memset( dis, , sizeof( dis ) );
dis[s] = v;
if( Bellman_Ford() )
cout << "NO" << endl;
else cout << "YES" << endl;
} return ;
}
POJ-1860 Currency Exchange( Bellman_Ford, 正环 )的更多相关文章
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
题目链接: https://cn.vjudge.net/problem/POJ-1860 Several currency exchange points are working in our cit ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
随机推荐
- 脱壳系列_0_FSG壳_详细版
---恢复内容开始--- 1 查看信息 使用ExeInfoPe查看此壳程序 可以看出是很老的FSG壳. 分析: Entry Point : 000000154,熟悉PE结构的知道,入口点(代码)揉进P ...
- 从深处去掌握数据校验@Valid的作用(级联校验)
每篇一句 NBA里有两大笑话:一是科比没天赋,二是詹姆斯没技术 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380) ...
- 深入理解HashMap(jdk7)
HashMap的结构图示 jdk1.7的HashMap采用数组+单链表实现,尽管定义了hash函数来避免冲突,但因为数组长度有限,还是会出现两个不同的Key经过计算后在数组中的位置一样,1.7版本 ...
- 【Android】Field requires API level 4 (current min is 1): android.os.Build.VERSION#SDK_INT
刚遇到了这个问题: Field requires API level 4 (current min is 1): android.os.Build.VERSION#SDK_INT 解决方法: 修改 A ...
- MySQL 之 Explain 输出分析
MySQL 之 Explain 输出分析 背景 前面的文章写过 MySQL 的事务和锁,这篇文章我们来聊聊 MySQL 的 Explain,估计大家在工作或者面试中多多少少都会接触过这个.可能工作中 ...
- 作为前端的你,CC游戏开发可以上车
1. 初来乍到 打开 Cocos Creator 点击新建空白项目,在默认布局的左下区域,一个黄黄assets文件夹映入眼帘.作为前端的你对这个文件是不是再熟悉不过了.是的,和你想象的一样,开发游戏中 ...
- 探秘最小生成树&&洛谷P2126题解
我在这里就讲两种方法 Prim 和 Kruscal Kruscal kruscal的本质其实是 排序+并查集 ,是生成树中避圈法的推广 算法原理如下 (1)将连通带权图G=<n,m>的各条 ...
- java多线程基础(一)--sleep和wait的区别
sleep和wait的区别有: 1.这两个方法来自不同的类分别是Thread和Object: 2.最主要是sleep方法没有释放锁,而wait方法释放了锁,使得线程可以使用同步控制块或者方法: 3.w ...
- ssm执行流程
SSM运行流程 1:服务器启动,创建springmvc的前端控制器DispatcherServlet,创建Spring容器对象. 加载spring-servlet.xml .applicationCo ...
- element ui 登录验证,路由守卫
<template> <!-- el-form :label-position="labelPosition" 设置label的位置 :model 用来给表单设置 ...