题目链接:

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. Python day 4

    阅读目录 内容回顾: 流程控制: if分支结构: while循环控制: for循环(迭代器): ##内容回顾: #1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 ...

  2. kbmmw 做REST 服务签名认证的一种方式

    一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...

  3. ABP框架系列之三十一:(Localization-本地化)

    Introduction Any application has at least one language for user interface. Many applications have mo ...

  4. openstack之cinder_backup对接ceph存储

    M版openstack,是kolla部署的 1.介绍 backup 功能好像与 snapshot 很相似,都可以保存 volume 的当前状态,以备以后恢复.但二者在用途和实现上还是有区别的,具体表现 ...

  5. MFC 字体

    dc.DrawText(_T("hello"), -1, //全部 &rect, DT_SINGLELINE| //在一行 DT_CENTER| //水平居中 DC_VCE ...

  6. 使用百度地图实现详细地址自动补全(补全bug''事件只能绑定到一个上的问题')

    function G(id) { return document.getElementById(id); } loadMapAutocomplete("suggestId",&qu ...

  7. (转载)RHEL7(RedHat 7)本地源的配置

    配置yum源 1.首先连接RedHat7的DVD再把挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt ...

  8. [BOT]自定义ViewPagerStripIndicator

    效果图 app中下面这样的控件很常见,像默认的TabHost表现上不够灵活,下面就简单写一个可以结合ViewPager切换内容显示,提供底部"滑动条"指示所显示页签的效果. 这里控 ...

  9. 第二十五节:Java语言基础-面向对象基础

    面向对象 面向过程的代表主要是C语言,面向对象是相对面向过程而言,Java是面向对象的编程语言,面向过程是通过函数体现,面向过程主要是功能行为. 而对于面向对象而言,将功能封装到对象,所以面向对象是基 ...

  10. 第二十一节:Java语言基础-关键字,标识符,注释,常量和变量,运算符

    Java语言基础-关键字,标识符,注解,常量和变量,运算符 class Demo { public static void main(String[] args){ System.out.printl ...