POJ 1860 Currency Exchange(Bellman-Ford)
https://vjudge.net/problem/POJ-1860
题意:
有多种货币,可以交换,有汇率并且需要支付手续费,判断是否能通过不断交换使本金变多。
思路:
Bellman-Ford算法。
在此之前对贝尔曼-福特算法没怎么了解,当边的权值存在负值的情况下,就可以使用贝尔曼-福特算法。
这个算法主要分为三个部分:
1、首先初始化,和Dijkstra一样,记录原点到各个点的距离,将原点的值设为0,其余点设为无穷大。
2、有n个点的情况下,最多只需要遍历n-1次,每次遍历所有边,进行松弛计算。也就是if(d(v)>d(u)+w(u,v)),d(v)=d(u)+w(u,v)。
3、第三部分就是用来检测是否存在复环的,最后再遍历一次所有边,如果存在d(v)>d(u)+w(u,v),则说明存在负环。
回到这道题,这道题目的话就是求一个最大路径,也就是松弛计算的时候计算更大值,最后判断是否存在正环,如果存在,则说明是可以变多的。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<set>
#include<map>
using namespace std; const int maxn = + ; int n, m, s; //货币总数、兑换点数量、有第s种货币
double v; //持有的s货币本金
int cnt; double d[maxn]; struct node
{
int a;
int b;
double rate;
double c;
}edge[maxn]; bool bellman_ford()
{
memset(d, , sizeof(d));
d[s] = v; bool flag;
for (int i = ; i <= n - ; i++)
{
flag = false;
for (int j = ; j < cnt; j++)
{
if (d[edge[j].b] < (d[edge[j].a] - edge[j].c)*edge[j].rate)
{
d[edge[j].b] = (d[edge[j].a] - edge[j].c)*edge[j].rate;
flag = true;
}
}
if (!flag) break;
} for (int j = ; j < cnt;j++)
if (d[edge[j].b] < (d[edge[j].a] - edge[j].c)*edge[j].rate)
return true;
return false;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int a, b;
double rab, cab, rba, cba;
while (~scanf("%d%d%d%lf", &n, &m, &s, &v))
{
cnt = ;
for (int i = ; i < m; i++)
{
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
edge[cnt].a = a;
edge[cnt].b = b;
edge[cnt].rate = rab;
edge[cnt++].c = cab;
edge[cnt].a = b;
edge[cnt].b = a;
edge[cnt].rate = rba;
edge[cnt++].c = cba;
}
if (bellman_ford())
printf("YES\n");
else
printf("NO\n");
}
return ;
}
POJ 1860 Currency Exchange(Bellman-Ford)的更多相关文章
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- POJ 1860 Currency Exchange(SPFA+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)
链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...
- POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
题目链接: https://cn.vjudge.net/problem/POJ-1860 Several currency exchange points are working in our cit ...
- POJ 1860 Currency Exchange(最短路&spfa正权回路)题解
题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- Currency Exchange(最短路)
poj—— 1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29851 Ac ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
随机推荐
- 安装Hadoop系列 — 安装SSH免密码登录
配置ssh免密码登录 1) 验证是否安装ssh:ssh -version显示如下的话则成功安装了OpenSSH_6.2p2 Ubuntu-6ubuntu0.1, OpenSSL 1.0.1e 11 ...
- Unity3D笔记十二 游戏元素二之摄像机
一.摄像机 摄像头用以捕捉和显示的世界给玩家.通过自定义和操纵相机,你可以让你的游戏的呈现真正独一无二的.您可以在一个场景无限数量的摄像机.它们可以设置在任何顺序呈现在屏幕上的任何地方,或者屏幕的某些 ...
- Unity3D笔记六 GUI游戏界面
1.Label:标签控件,可以在游戏中用来展示文本字符串信息,不仅可以写字还可以贴图片. 2.Button:按钮控件,一般分图片按钮和普通的按钮,还有一个连续按钮RepeatButton注意,这个在W ...
- windows下resin的配置部署与调试
配置 从Resin官网(http://www.caucho.com)下载Resin解压后,启动Resin,运行resin根目录下的resin.exe文件,运行期间将出现下图所示的命令提示符窗口. 表示 ...
- PreparedStatement vs Statement
Statement和PreparedStatement之间的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statemen ...
- poj2376 Cleaning Shifts【线段树】【DP】
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32561 Accepted: 7972 ...
- hdu6386 Age of Moyu【最短路】
Age of Moyu Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) To ...
- MySQL Bugs: #34354: Feature request: EXPLAIN ALTER TABLE https://bugs.mysql.com/bug.php?id=34354
MySQL Bugs: #34354: Feature request: EXPLAIN ALTER TABLE https://bugs.mysql.com/bug.php?id=34354 [SQ ...
- timedatectl — Control the system time and date
timedatectl --help 的执行结果如下: timedatectl [OPTIONS...] COMMAND ... Query or change system time and dat ...
- linux内存管理之vmalloc函数分析
2017-07-09 今天周末,闲来无事聊聊linux内核内存分配那点事……重点在于分析vmalloc的执行 流程 以传统x86架构为例,内核空间内存(3G-4G)主要分为三大部分:DMA映射区,一致 ...