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 ...
随机推荐
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- CEPH RGW多 ZONE的配置
相关的名称解释 Region :可以理解为区域,是基于地理位置的逻辑划分:如:华南,华北之类,包含多个region的Ceph集群必须指定一个master region,一个region可以包含一个或者 ...
- .net core使用ocelot---第一篇 简单使用
简介原文地址 接下来你会学习,基于asp.net core 用Ocelot实现一个简单的API网关.或许你会疑问什么是API网关,我们先看下面的截图 API网关是访问你系统的入口,它包括很多东西,比如 ...
- 【Java例题】8.1手工编写加法器的可视化程序
1. 手工编写加法器的可视化程序. 一个Frame窗体容器,布局为null,三个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器ActionLis ...
- X-Admin&ABP框架开发-设置管理
在网站开发中,设置是不可缺少的一环,如用户设置.系统设置.甚至是租户设置等.ABP对于设置的管理已经做了很好的处理,我们可以借助巨人的力量来完成我们的冒险. ABP官网地址:https://aspne ...
- Window.open使用总结
前言 今天在项目中,突然看到window.open的使用,感觉还是很神奇,突然心血来潮查看了window.open的用法. 用途 主要用于在打开网站时弹出的其他窗口.用于通知广告一类的. 用法 win ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- Daily,一个入门级的 React Native 应用
Daily,一个React-Native写的android app. 下拉刷新获取:图片.诗句.言语.音乐.乐评.雨声.知乎日报.历史上的今天. 可以说是一个入门级的React-Native应用. 项 ...
- 服务注册组件——Eureka高可用集群搭建
服务注册组件--Eureka高可用集群搭建 什么是Eureka? 服务注册组件:将微服务注册到Eureka中. 为什么需要服务注册? 微服务开发重点在一个"微"字,大型应用拆分成微 ...