题意:有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. Oracle composite index column ordering

    Question:  I have a SQL with multiple columns in my where clause.  I know that Oracle can only choos ...

  2. Interface Comparator

    int compare(T o1, T o2) Compares its two arguments for order. Returns a negative integer, zero, or a ...

  3. Android学习及开发随记1:Android Studio安装配置

    1.本系列仅为个人使用,概不负责.随着时间推移,部分内容可能因为软件更新而出现不能对应的情况. 本文的配置情况,仅针对Android Studio v1.1.0 windows版本 全新安装. Goo ...

  4. map的详细用法

     map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  5. Android:Context的作用

    Context字面意思上下文,Activity中我们直接用this代替,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,改用ActivityName.t ...

  6. Linux Shell脚本入门:tee命令

    用途说明   在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee ...

  7. highcharts 结合phantomjs纯后台生成图片

    highcharts 结合phantomjs纯后台生成图片 highcharts 这个图表展示插件我想大家应该都知道,纯javascript编写,相比那些flash图表插件有很大的优势,至少浏览器不用 ...

  8. OpenCV源码阅读(2)---matx.h---函数的内联实现

    外部矩阵计算函数 namespace internal { template<typename _Tp, int m> struct Matx_DetOp { double operato ...

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

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

  10. ehcache版本冲突

    以ehchache-core2.5为分水岭 缓存版本问题 版本不一样 配置不一样  ehcache-core-2.4.3.jar 与 ehcache-core-2.6.6 一 Caused by: n ...