题意:有n种货币,可以互相兑换,有m个兑换规则,兑换规则给出汇率r和手续费c,公式为b = (a - c) * r,从a货币兑换为b货币,问能不能通过不断的兑换赚钱,兑换期间手中的钱数不可以为负。

解法:Bellman-Ford。建图:将货币看做点,每种兑换规则为边,两点的路径长度为兑换后的钱数。建图之后可以看出题意为求图中是否存在正环,用Bellman-Ford求最长路径,如果存在正环输出YES,不存在输出NO。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; struct node
{
int u, v;
double r, c;
node(int u, int v, double r, double c) : u(u), v(v), r(r), c(c) {}
node() {}
}edge[205];
int n, m, st, cnt;
double money;
double dis[105];
bool BellmanFord()
{
for(int i = 1; i <= n; i++)
{
if(i == st) dis[i] = money;
else dis[i] = 0;//初始化为0,因为过程中不可以有负金额
}
for(int i = 1; i < n; i++)//进行n-1次松弛
for(int j = 0; j < cnt; j++)
dis[edge[j].v] = max((dis[edge[j].u] - edge[j].c) * edge[j].r, dis[edge[j].v]);
for(int i = 0; i < cnt; i++)//如果用n-1条边之后还可以进行松弛则说明存在正环
if(dis[edge[i].v] < (dis[edge[i].u] - edge[i].c) * edge[i].r) return 1;
return 0;
}
int main()
{
while(~scanf("%d%d%d%lf", &n, &m, &st, &money))
{
cnt = 0;
for(int i = 0; i < m; i++)
{
int u, v;
double r1, c1, r2, c2;
scanf("%d%d%lf%lf%lf%lf", &u, &v, &r1, &c1, &r2, &c2);
edge[cnt++] = node(u, v, r1, c1);
edge[cnt++] = node(v, u, r2, c2);
}
if(BellmanFord()) puts("YES");
else puts("NO");
}
return 0;
}

  

POJ 1860 Currency Exchange的更多相关文章

  1. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  2. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  3. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  4. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  5. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

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

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

  9. POJ 1860 Currency Exchange (Bellman-Ford)

    题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...

  10. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

随机推荐

  1. 因SELinux引起的用户登录问题解决案例

    增强安全性带来的负作用往往是牺牲便利性,就像北京地铁的安检一样,但有些时候我们确实需要它.   案例是,用户有一台安装了KylinOS(国产麒麟,使用的是redhat的内核)的系统,当我们对其系统文件 ...

  2. PHP7 扩展之自动化测试

    在安装 PHP7 及各种扩展的过程中,如果你是用源码安装,会注意到在 make 成功之后总会有一句提示:Don't forget to run 'make test'. 这个 make test 就是 ...

  3. 0到N数其中三个数的全排列

    #include<iostream> using namespace std; int main(){ ; int count; count=; ;i<=N;i++) ;j<= ...

  4. Ubuntu环境下Nutch+Tomcat 搭建简单的搜索引擎

    简易的搜索引擎搭建 我的配置: Nutch:1.2 Tomcat:7.0.57 1 Nutch设置 修改Nutch配置 1.1 修改conf/nutch-site.xml <?xml versi ...

  5. [主席树]SPOJ DQUERY

    题目链接 题意:n个数 m个查询 查询的是[l, r]区间内不相同的数的个数 没有修改,因此静态的主席树就好了 将重复的元素建树即可 query的时候加起来,用区间长度(r-l+1)去减就是答案 (q ...

  6. cojs 简单的最近公共祖先 解题报告

    我曾经自己想过每考试一次就从考试题中找找idea来出题 这次又找到了一个,先不管原来的考试题是什么 考试题中其中的一部分就是今天的这道题目啦 当时考场上自己比较傻,没有注意到有用的性质,套用了之前黑白 ...

  7. PHP输入流php://input [转]

    对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...

  8. Linux之SAMBA共享服务

    简述 Samba服务器可以让Windows操作系统用户访问局域网中Linux主机,就象访问网上邻居一样方便. 具有以下功能: 共享目录:在局域网上共享某个或某些目录,使得同一个网络内的Windows用 ...

  9. ASP.NET中App_Code,App_Data等文件夹的作用

    http://www.cnblogs.com/shiyu007/archive/2007/12/04/982264.html 1.  Bin文件夹 Bin文件夹包含应用程序所需的,用于控件.组件或者需 ...

  10. shell 编程基础(1)---初识shellscript

    shellscript 是linux下强大的系统管理工具,可以通过bash命令和管道命令直接在linux系统上进行编程,所写的脚本不需要编译就可以执行,对于系统管理而言十分方便. #!/bin/bas ...