传送门:点击打开链接

题目大意:一个城市有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判断正环)的更多相关文章

  1. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  2. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  3. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  4. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  6. POJ-1860 Currency Exchange---Bellman-Ford判断正环

    题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑 ...

  7. poj1860 Currency Exchange(spfa判断正环)

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  8. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  9. poj 1860 (Bellman_Ford判断正环)

    题意:给出n种货币,m中交换关系,给出两种货币汇率和手续费,求能不能通过货币间的兑换使财富增加. 用Bellman_Ford 求出是否有正环,如果有的话就可以无限水松弛,财富可以无限增加. #incl ...

随机推荐

  1. maven依赖scope配置项讲解

    我们在使用Maven配置依赖项的时候,常常只会配置Maven的坐标以及版本信息就可以了,但我们看其他人的工程代码的时候常常会见到有个scope配置项,今天就来分别介绍下这个配置下几个类别的作用. &l ...

  2. Linux 查看一个端口的连接数

    netstat -antp|grep -i "80" |wc -l 譬如查看80端口的连接数

  3. solr通过http请求搜索

    请求搜索必要的条件是:设置搜索条件params 设置 1.简单条件 SolrParams params = new SolrQuery("name:小飞鸟 AND  id:1520" ...

  4. WINFORM 开发模式,窗体回到默认样式方法。

    软件分为3类   客户端  网站应用  app WINFORM 主要用来只做客户端应用程序.C/S 客户端程序很重要的特点:可以操作用户电脑上的文件,执行在客户端上,电脑的配置越高执行就越流畅. 在p ...

  5. ZROI #88

    传送门 分析 我们考虑把每个A[i]考虑为山峰的高度,每次的B考虑为海平面 于是我们知道对于A[i]和A[i-1],如果A[i-1]<A[i]则在A[i-1]<B<=A[i]时会使陆 ...

  6. python产生随机字符串

    def GenerateRandomString(len, basechars = []): if (basechars == []): x = range(ord() x.extend(range( ...

  7. Visual Studio 代码格式化插件(等号自动对齐、注释自动对齐等)

    1.下载地址 插件:Code alignment  下载地址 2.介绍 Based on principles borrowed from mathematics and other discipli ...

  8. WPF之MVVM模式(2)

    我们都想追求完美 Every view in the app has an empty codebehind file, except for the standard boilerplate cod ...

  9. 完整读写txt 并提取{}里的内容

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  10. 在GridView控件FooterTemplate内添加记录

    在GridView控件FooterTemplate内添加记录,想实现这个功能,有几点要清楚的,这个添加铵钮是在FooterTemplate内,还是在GridView控件外部,位置不同,某些处理逻辑会有 ...