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 ...
随机推荐
- iconfont阿里爸爸做的开源图库
iconfont 三种使用姿势 1.unicode格式 优点 兼容性最好,支持ie6+ 支持按字体的方式去动态调整图标大小,颜色等等 缺点 不支持多色图标 在不同的设备浏览器字体的渲染会略有差别,在不 ...
- java中生成流水号的一个例子(使用关系型数据库)
在实际的开发中,可能会有根据一定的规则生成流水号的需求(比如根据根据公司编码和日期生成4位流水号)我们可以把公司和日期联合起来作为一个业务编码,把这个业务编码和序列的值存储到数据库中,每次需要生成流水 ...
- 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆
[BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...
- {sharepoint} SetPermission
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- 如何通过命令在Ubuntu中安装PyCharm
对于Ubuntu 16.10和Ubuntu 17.04,通过Ctrl + Alt + T打开终端,或通过从应用启动器搜索“terminal”,打开后,执行以下步骤: 安装: 1.通过命令添加PPA存储 ...
- Oracle之rman命令的使用(51CTO风哥rman课程)
看rman的连接串的帮助 连接数据库 rman target/ rman的版本要和目标数据库一致(一般大版本可以往下兼容小版本) 运行操作系统命令 run {host "pwd"; ...
- 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】
You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...
- Intellij idea的Dependencies波浪线
昨天在家做项目不知道搞了什么出现了大量波浪线.搞了大半天解决了下面的问题. 1.pom.xml出现波浪线.看右边 maven project-->Profiles 勾选dev 2.上面已勾选还有 ...
- Python开发【Django】:Admin配置管理
Admin创建登录用户 数据库结构表: from django.db import models # Create your models here. class UserProfile(models ...
- 关于sed -i 修改selinux 的软链接文件的问题
关于sed -i 修改selinux 的软链接文件的问题 http://blog.csdn.net/kumu_linux/article/details/8598005 因为sed -i /etc/s ...