poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接
题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多。
思路:一开始以为一个地方只能用一次,感觉好像有点难,后来发现自己读错题了,其实只要判断给你的这幅图存不存在正环就可以了,用dis【】表示某种货币的数量,然后bellman判断正环就可以了。(题目里强调结尾一定要原来的货币,但其实这是废话,因为是以原来的货币为起点的,所以你换出去了一定换的回来),正环指的是跑一圈w变大的环。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<cmath>
#include<time.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;
int n,m,s,num;
const int maxn=110;
double V,dis[maxn];
struct edge{
int u,v;
double cost,w;
}e[220];
void addv(int a,int b,double ra,double ca){
e[num].u=a;
e[num].v=b;
e[num].cost=ca;
e[num++].w=ra;
}
bool bellman(){
dis[s]=V;
for(int i=1;i<n;i++){//松弛n-1次
bool flag=false;
for(int j=0;j<num;j++){
int u=e[j].u;
int v=e[j].v;
if(dis[v]<(dis[u]-e[j].cost)*e[j].w){
dis[v]=(dis[u]-e[j].cost)*e[j].w;
flag=true;
}
}
if(!flag)return false;// 如果n-1次都无法松弛 那肯定不存在正环
}
for(int i=0;i<num;i++){
if(dis[e[i].v]<(dis[e[i].u]-e[i].cost)*e[i].w)//第n次若能松弛 说明存在正环
return true;
}
return false;
}
int main(){
while(scanf("%d%d%d%lf",&n,&m,&s,&V)!=EOF){
num=0;
sizeof(dis,0,sizeof(dis));
while(m--){
int a,b;
double ra,ca,rb,cb;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&ra,&ca,&rb,&cb);
addv(a,b,ra,ca);
addv(b,a,rb,cb);
}
if(bellman())printf("YES\n");
else printf("NO\n");
}
}
poj1860 兑换货币(bellman ford判断正环)的更多相关文章
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- POJ-1860 Currency Exchange---Bellman-Ford判断正环
题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑 ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- poj 1860 (Bellman_Ford判断正环)
题意:给出n种货币,m中交换关系,给出两种货币汇率和手续费,求能不能通过货币间的兑换使财富增加. 用Bellman_Ford 求出是否有正环,如果有的话就可以无限水松弛,财富可以无限增加. #incl ...
随机推荐
- hbase性能调优(转载)
一.服务端调优 1.参数配置 1).hbase.regionserver.handler.count:该设置决定了处理RPC的线程数量,默认值是10,通常可以调大,比如:150,当请求内容很大(上MB ...
- JavaScript Array.map
Array.prototype.map() History Edit This article is in need of a technical review. Table of Contents ...
- 数据库理论-范式(1NF、2NF、3NF)
范式是“符合某一种级别的关系模式的集合,表示一个关系内部各属性之间的联系的合理化程度”. 第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项.(每个属性不可分割)第二范式(2NF)要求数据 ...
- 奇葩问题 eclipse下 maven项目 java Resource报个小红叉,然而里面却没有小红叉
之前没注意,不知是一开始就有还是这两天才有,说下解决方案: 右击项目“Properties”,在弹出的“Properties”的左侧边框,单击“Project Facets”,打开“Project F ...
- iOS 隐藏App图标
1.在进入Info.plist文件 2.在Info.plist文件中新添加一项,把Key值设置为SBAppTags,在Type选项中选取Array 3.在Array中新添加一项Item0,Type类型 ...
- Luogu 2322 [HNOI2006]最短母串问题
唔,太菜了,弄了好几个小时. 状压dp,设$f_{s, i}$表示选了集合$s$,以$i$结尾的最短长度,设$g_{i, j}$表示串$i$的后缀和串$j$的前缀的最长匹配长度. $f_{s, i} ...
- Entity Framework Tutorial Basics(35):Local Data
Local Data The Local property of DBSet provides simple access to the entities that are currently bei ...
- 多线程学习-基础( 十)一个synchronized(){/*代码块*/}简单案例分析
一.提出疑惑 上一篇文章中,分析了synchronized关键字的用法.但是好像遗漏了一种情况. 那就是: synchronized(obj){/*同步块代码*/} 一般有以下几种情况: (1)syn ...
- Visual Studio 2012自动添加注释(如版权信息等)
http://blog.csdn.net/jiejiaozhufu/article/details/16357721注释宏的原码 /********************************** ...
- Java50道经典习题-程序38 求字符串长度
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. import java.util.Scanner; public class Prog38 { public stat ...