POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)
题目链接:
https://cn.vjudge.net/problem/POJ-1860
For example, if you want to exchange 100 US Dollars into
Russian Rubles at the exchange point, where the exchange rate is 29.75,
and the commission is 0.39 you will get (100 - 0.39) * 29.75 =
2963.3975RUR.
You surely know that there are N different currencies you can
deal with in our city. Let us assign unique integer number from 1 to N
to each currency. Then each exchange point can be described with 6
numbers: integer A and B - numbers of currencies it exchanges, and real R
AB, C
AB, R
BA and C
BA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can
somehow, after some exchange operations, increase his capital. Of
course, he wants to have his money in currency S in the end. Help him to
answer this difficult question. Nick must always have non-negative sum
of money while making his operations.
Input
of currencies, M - the number of exchange points, S - the number of
currency Nick has and V - the quantity of currency units he has. The
following M lines contain 6 numbers each - the description of the
corresponding exchange point - in specified above order. Numbers are
separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100,
V is real number, 0<=V<=10
3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10
-2<=rate<=10
2, 0<=commission<=10
2.
Let us call some sequence of the exchange operations simple
if no exchange point is used more than once in this sequence. You may
assume that ratio of the numeric values of the sums at the end and at
the beginning of any simple sequence of the exchange operations will be
less than 10
4.
Output
Sample Input
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES
/*
题意描述
输入货币的种数n,货币兑换点数m,某人有货币种类s,价值hm
m个兑换点的规则分别是a兑换b,汇率是rab,费用是cab,b兑换a,汇率是rba,费用是cba,计算规则是(va-cab)*rab
问能否通过这m个兑换点使他的拥有s这种货币的钱数增加 解题思路
不管怎么兑换,关键是最后还要换回s这种钱,那么必须至少存在一个环,使得最后还能换回s,但是又要求增加钱数,那么这个换必须是正
环。所以问题变成了如何判断图中存在正环。使用Bellman_Ford算法判断正环即可。 样例中第二条边是1.10 不是1.00,看样例看了半天没看懂
注意函数调用时数据类型的变换,包括输入和函数传递参数时的数据类型
*/
#include<cstdio>
#include<cstring>
const int maxn = ; int u[maxn], v[maxn];
double ruv[maxn], cuv[maxn], dis[maxn];
int n, m, en;//边数
bool Bellman_Ford(int s, double hm); int main()
{
int a, b, s;
double hm, rab, cab, rba, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &hm) != EOF) {
en = ;
for(int i = ; i <= m; i++) {
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
u[en] = a; v[en] = b;
ruv[en] = rab;
cuv[en++] = cab; u[en] = b; v[en] = a;
ruv[en] = rba;
cuv[en++] = cba;
} if(Bellman_Ford(s, hm))
puts("YES");
else
puts("NO");
}
return ;
} bool Bellman_Ford(int s, double hm) {
memset(dis, , sizeof(dis));
dis[s] = hm; for(int i = ; i <= n; i++) {
for(int j = ; j < en; j++) {
if(dis[v[j]] < (dis[u[j]] - cuv[j]) * ruv[j]) {
dis[v[j]] = (dis[u[j]] - cuv[j]) * ruv[j];
if(i == n)
return ;//存在正环
}
}
}
return ;
}
使用结构体封装一下Bellman-Ford算法,再使用队列和邻接表优化一下,代码如下:
使用的时候注意数据类型的使用和结点数全部减1。
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std; const int maxn = ; struct Edge {
int from, to;
double rait, com;
Edge(int u, int v, double r, double c): from(u), to(v), rait(r), com(c) { }
}; struct Bellman_Ford {
int n, m, s;
double hm;
vector<Edge> edges;
vector<int> G[maxn];
double d[maxn];
bool inq[maxn];
int cnt[maxn]; void init(int n) {
this->n = n;
for(int i = ; i < n; i ++) {
G[i].clear();
}
edges.clear();
} void AddEdge(int from, int to, double rait, double com){
edges.push_back(Edge(from, to, rait, com));
m = edges.size();
G[from].push_back(m - );
} bool bellman_ford (int s, double hm) {
this->s = s;
this->hm = hm;
memset(d, , sizeof(d));
d[s] = hm; memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt)); queue<int> q;
q.push(s);
inq[s] = ; while(!q.empty()) {
int u = q.front();
q.pop(); inq[u] = ;
for(int i = ; i < G[u].size(); i++) {
Edge e = edges[G[u][i]];
if(d[e.to] < (d[u] - e.com) * e.rait){
d[e.to] = (d[u] - e.com) * e.rait;
if(!inq[e.to]) {
q.push(e.to);
inq[e.to] = ; cnt[e.to]++;
if(cnt[e.to] > n)
return ;
}
}
}
}
return ;
}
}; struct Bellman_Ford solve;
int main()
{
int s, a, b, n, m;
double hm, rab, cab, rba, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &hm) != EOF) {
solve.init(n);
for(int i = ; i <= m; i++) {
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
a--;
b--;
solve.AddEdge(a, b, rab, cab);
solve.AddEdge(b, a, rba, cba);
}
int ans = solve.bellman_ford(s-,hm); if(ans)
puts("YES");
else
puts("NO");
}
return ;
}
POJ 1860 Currency Exchange(如何Bellman-Ford算法判断图中是否存在正环)的更多相关文章
- POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
随机推荐
- Mac 电脑设置显示路径
# 设置 defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;killall Finder # 删除 defaults ...
- pycharm 2017最新激活码
BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- Chapter3_操作符_别名机制
Java中的别名机制实际体现的是对于“=”这一类赋值操作符的使用规则和内涵.“=”的实际内涵是指将右边的变量的值(对于基本数据类型而言)或者某一个对象的引用(对于某个具体对象而言)复制到左边的变量名所 ...
- HTML-表格-列表-结构标记-表单
1.表格 1.表格语法 1.标记 1.表格 <table></table> 2.行 <tr></tr& ...
- Django中的缓存(内存,文件,redis)
一.Django中的缓存的几种方法 1)单个视图缓存.时间测试 import time from django.views.decorators.cache import cache_page @ca ...
- xpath获取一个标签下的多个同级标签
一.问题: 我在使用xpath获取文章内容的时候会遇到,多个相同的标签在同一级下面,但是我们只需要获取一部分的内容.比如我不想需要原标题这些内容. 二.解决: Xpath中有一个position()的 ...
- node.js获取参数的常用方法
1.req.body 2.req.query 3.req.params 一.req.body例子 body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body, ...
- Spring通过在META-INF/spring.handlers中的属性进行配置文件解析
在Spring的入口函数refresh()之中进行的. AbstractApplicationContext ConfigurableListableBeanFactory beanFactory = ...
- CCNA笔记
*****************交换机********************一:交换机:具有多个交换端口,专用集成电路技术使得交换器以线路速率在所有的端口并行转发数据,有很高的总体吞吐率;虚拟网V ...
- AdminLTE用django部署
前言 最近从网上看到AdminLTE这个web前端的主题挺好的,我平时用python就是写一些后台,准备以后就用这个框架了,这里就是把这个用django初始化一下这个项目. 基础环境介绍 Python ...