poj1860(Bellman—fold)
题目连接: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
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 题目大意:有若干种货币,若干个兑换点,每个兑换点可以把一种货币兑换为另一种货币(可A->B,也可B->A),但是兑换有佣金,假设把A变为B,汇率为r,佣金为c,则B=(A-c)*r。给出这些兑换点的信息 以及 初始的钱的种类和数量,求是否可能进过若干次兑换使钱(最后必须是最开始的币种)变多;解题思路:转化为图,货币为节点,兑换点为边,则构成一个无向图,而问题就转化成了求次无向图是否存在正环(因为最后要化成开始的币种,而不是价值变多即可,所以是求正环)用Bellman——fold算法的思想,可以无限松弛即为正环,就可以解决了(原算法为求负环,只需把初始化的状态和松弛条件改一下即可)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
struct Edge
{
int from,to;
double r,c;
Edge(int u,int v,double r,double c):from(u),to(v),r(r),c(c) {}
};
vector<];
vector<Edge> edges;
]= {};
]= {};
];
int n;
bool bellman_fold(int s,double value)
{
queue<int> Q;
memset(d,,sizeof(d));
Q.push(s);
d[s]=value;
inq[s]=;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=;
; i<G[u].size(); i++)
{
int now=G[u][i];
Edge & e=edges[now];
&&d[e.to]<(d[u]-e.c)*e.r)
{
d[e.to]=(d[u]-e.c)*e.r;
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=;
if(++cnt[e.to]>n)
;
}
}
}
}
;
}
int main()
{
int m,no;
;
double sum;
cin>>n>>m>>no>>sum;
while(m--)
{
int no1,no2;
double rab,cab,rba,cba;
scanf("%d%d%lf%lf%lf%lf",&no1,&no2,&rab,&cab,&rba,&cba);
edges.push_back(Edge(no1,no2,rab,cab));
G[no1].push_back(x);
x++;
edges.push_back(Edge(no2,no1,rba,cba));
G[no2].push_back(x);
x++;
}
bool flag = bellman_fold(no,sum);
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
poj1860(Bellman—fold)的更多相关文章
- POJ1860(Currency Exchange)
题意: 给出一张各种货币交换的网络,问在网络中交换原有的货币,问货币能否增值? 解析: 判断是否存在正环即可 用spfa 负环和正环的判定方法一样 如果一个点的进队次数超过n次 则存在环 代码如 ...
- [笔记]LibSVM源码剖析(java版)
之前学习了SVM的原理(见http://www.cnblogs.com/bentuwuying/p/6444249.html),以及SMO算法的理论基础(见http://www.cnblogs.com ...
- LibSVM源码剖析(java版)
之前学习了SVM的原理(见http://www.cnblogs.com/bentuwuying/p/6444249.html),以及SMO算法的理论基础(见http://www.cnblogs.com ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- LibLinear(SVM包)使用说明之(一)README
转自:http://blog.csdn.net/zouxy09/article/details/10947323/ LibLinear(SVM包)使用说明之(一)README zouxy09@qq.c ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- 从零开始学ios开发(十八):Storyboards(下)
这篇我们完成Storyboards的最后一个例子,之前的例子中没有view之间的切换,这篇加上这个功能,使Storyboards的功能完整呈现.在Storyboards中负责view切换的东西叫做“s ...
- linux —— shell 编程(文本处理)
导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...
- erlang程序优化点的总结(持续更新)
转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多 ...
随机推荐
- HTML5<canvas>标签:使用canvas元素在网页上绘制四分之一圆(3)
前几天自己做了个四分之一的圆,放到手机里面测试.效果不是很好.于是今天通过查资料,找到了canvas.自己研究了一天,发现可以使用canvas画圆.代码如下: <!doctype html> ...
- BZOJ4481 JSOI2015非诚勿扰(概率期望+树状数组)
首先求出每个女性接受某个男性的概率.这个概率显然是一个无穷等比数列求和. 然后按编号从小到大考虑每个女性,维护出每个男性被选择的期望次数,BIT上查询后缀和即可. 需要long double. #in ...
- 【题解】HNOI2009无归岛
这题真的是无语了,在哪个岛上根本就没有任何的用处……不过我是画了下图,感受到一定是仙人掌,并不会证.有谁会证的求解…… 如果当做仙人掌来做确实十分的简单.只要像没有上司的舞会一样树形dp就好了,遇到环 ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 杭电hdu 2089 数位dp
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...
- oracle的rownum使用
对于rownum来说它是Oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...
- python构建一个项目
二.实验步骤 2.1 实验准备 我们的实验项目名为 factorial. $ mkdir factorial $ cd factorial/ 2.2 主代码 我们给将要创建的 Python 模块取名为 ...
- PHP设计模式-代理模式
概念理解: 代理模式,是对简单处理程序(或指针)的增强,用于引用一个对象:这个指针被代理对象取代,代理对象位于客户端和真实程序之间,指针有一个可被多个目标利用的钩子. 参与者: client(参与者) ...
- python实现后台系统的JWT认证
介绍一种适用于restful+json的API认证方法,这个方法是基于jwt,并且加入了一些从oauth2.0借鉴的改良. 1. 常见的几种实现认证的方法 首先要明白,认证和鉴权是不同的.认证是判定用 ...
- HDU 2084 DP经典例子---数塔问题
http://acm.hdu.edu.cn/showproblem.php?pid=2084 #include "iostream" #include "cstdio&q ...