题目链接:

https://cn.vjudge.net/problem/POJ-1860

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
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

The first line of the input contains four numbers: N - the number
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

If Nick can increase his wealth, output YES, in other case output NO to the output file.

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算法判断图中是否存在正环)的更多相关文章

  1. POJ 1860 Currency Exchange (Bellman-Ford)

    题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...

  2. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  3. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  4. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  5. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  6. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  7. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  8. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  9. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

随机推荐

  1. M1卡知识点描述

  2. 利用python监测linux上的服务(简单实现服务宕掉自动发送邮件)

    python 这里用到了四个python 模块 : import time (时间模块) import re (正则模块) import socket (监测端口模块)import yagmail ( ...

  3. VS“新建网站”与“新建Asp.Net Web 应用程序”的区别

    WebApplication(新建Asp.Net Web 应用程序)编程模型的优点:针对大型网站 1.编译速度网站编译速度快,使用了增量编译模式,仅仅只有文件被修改后,这部分才会被增量编译进去. 2. ...

  4. 2019.02.12 bzoj5294: [Bjoi2018]二进制(线段树)

    传送门 题意简述: 给出一个长度为nnn的二进制串. 你需要支持如下操作: 修改每个位置:1变0,0变1 询问对于一个区间的子二进制串有多少满足重排之后转回十进制值为333的倍数(允许前导000). ...

  5. using python read/write HBase data

    A. operations on Server side 1. ensure hadoop and hbase are working properly 2. install thrift:  apt ...

  6. MySQL:索引

    索引的目的在于提高查询效率,它的作用就相当于一本书的目录: 1. 常见的索引模型 1.1 哈希表 优点:适用于等值查询的场景: 缺点:范围查询效率较低: 1.2 有序数组 优点:范围查询和等值查询效率 ...

  7. Linux - 查看命令所属的软件包

    这里以查看netstat命令所属的软件包为例. CentOS:利用yum provides命令 netstat命令所属的软件包为net-tools [root@CentOS7 ~]# yum prov ...

  8. vue.js多页面开发环境搭建

    利用 vue-cli 搭建的项目大都是单页面应用项目,对于简单的项目,单页面就能满足要求.但对于有多个子项目的应用,如果创建多个单页面,显示有点重复,特别是 node_modules 会有多份相同的. ...

  9. [EXP]phpBB 3.2.3 - Remote Code Execution

    // All greets goes to RIPS Tech // Run this JS on Attachment Settings ACP page var plupload_salt = ' ...

  10. Linux编程 8 (挂载mount,查看磁盘df du,搜索grep,压缩zgip,归档tar)

    一. 挂载存储媒体 linux文件系统将所有的磁盘都并入一个虚拟目录下,当使用新的存储媒体之前,需要把它放到虚拟目录下,这项工作称为挂载(mounting) 1.1 mount 命令 在linux上用 ...