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 ...
随机推荐
- Vue中出现Do not use built-in or reserved HTML elements as component id:footer等等vue warn问题
错误示图: 原因:是因为在本地项目对应文件的<script>中,属性name出现了错误的命名方式,导致浏览器控制台报错! 诸如: name: header . . name: menu ...
- 一不注意,在Unity3D中DllImport 引起的Bug.
单要说这个Bug是很简单,但是得从头说起. 一些大型的网络游戏,或者加载比较多的一些场景时,如果要等待所有模型,贴图等各种资源文件加载完毕才能执行游戏,对用户将会是一个很头大的事情.所以就需要用到动态 ...
- @RemoteProxy()注释 与@File注释的使用
@RemoteProxy()注释 dwr3.0可以通过全注解的方式,极大的简化了配置,所有xml配置加在一起不超过20行,而且使用更加简单,bean注入的问题也都解决.配置步骤如下: web.xml的 ...
- RDS MySQL InnoDB 锁等待和锁等待超时的处理
https://help.aliyun.com/knowledge_detail/41705.html 1. Innodb 引擎表行锁等待和等待超时发生的场景 2.Innodb 引擎行锁等待情况的处理 ...
- ios 单例的再次理解
单例模式 在建模的时候,如果这个东西确实只需要一个对象,多余的对象都是无意义的,那么就考虑用单例模式.比如定位管理(CLLocationManager),硬件设备就只有一 个,弄再多的逻辑对象 ...
- 虚拟机 django 端口无法连接
我的虚拟机django服务器为192.168.27.100,使用启动命令python manage.py runserver 9001启动后,发现笔记本电脑的游览器无法连接 python@qinhan ...
- 03-jQuery动画效果
Query提供的一组网页中常见的动画效果,这些动画是标准的.有规律的效果:同时还提供给我们了自定义动画的功能. 显示动画 方式一: $("div").show(); 解释:无参数, ...
- java实现随机产生6位数的方法总结
package com.yin.test; import java.util.Random; import org.junit.Test; /** * @author v_yinyl * @date ...
- 进程池、tornado、字体
协程: import grequests from fake_useragent import UserAgent urls=[f'http://bir删d.so/search?page={p ...
- hadoop2.4.0伪分布式搭建以及分布式关机重启后datanode没起来的解决办法
1.准备Linux环境 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet ip ...