POJ-1860 Currency Exchange---Bellman-Ford判断正环

题目链接:

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

题目大意:

我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。

例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布。

你肯定知道在我们的城市里你可以处理不同的货币。让每一种货币都用唯一的一个小于N的整数表示。然后每个交换点,可以用6个整数表描述:整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。

nick有一些钱在货币S,他希望能通过一些操作(在不同的兑换点兑换),增加他的资本。当然,他想在最后手中的钱仍然是S。帮他解答这个难题,看他能不能完成这个愿望。

输入数据:

第一行四个数,N,表示货币的总数;M,兑换点的数目;S,nick手上的钱的类型;V,nick手上的钱的数目;1<=S<=N<=100, 1<=M<=100, V 是一个实数 0<=V<=103.

接下来M行,每行六个数,整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金(0<=佣金<=102,10-2<=汇率<=102

输出数据:

如果nick能够实现他的愿望,则输出YES,否则输出NO。

思路:

这是bellman-ford算法的逆运用,首先初始化的时候dist数组全部为0,而源点的dist为题目给定的初始钱的数目,每次松弛的时候往大的方向松弛,而且松弛的时候是乘而不是加。直接判断第n次松弛时是否有更新。有更新说明存在正环

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<sstream>
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 200 + 10;
13 const int INF = 1 << 25;
14 int T, n, m, cases;
15 struct money
16 {
17     int u, v;
18     double r, c;//r为汇率,c为手续费
19     money(){}
20     money(int u, int v, double r, double c):u(u), v(v), r(r), c(c){}
21 };
22 money e[maxn];
23 double d[maxn];
24 int tot;
25 bool bellman(int u, double s)//u为源点,s为最初有的钱
26 {
27     memset(d, 0, sizeof(d));//这里需要正环,初始化时赋值为0
28     d[u] = s;//源点初始化成1
29     for(int i = 0; i < n; i++)//n次迭代
30     {
31         for(int j = 0; j < tot; j++)
32         {
33             int x = e[j].u, y = e[j].v;
34             double r = e[j].r, c = e[j].c;
35             if((d[x] - c) * r > d[y])//如果松弛结果更大,更新
36             {
37                 d[y] = (d[x] - c) * r;
38                 if(i == n - 1)return true;//说明存在正环
39             }
40         }
41     }
42     return false;
43 }
44 int main()
45 {
46     int s, a, b;//起点s
47     double v, rab, rba, cab, cba;
48     while(cin >> n >> m >> s >> v)
49     {
50         tot = 0;
51         for(int i = 0; i < m; i++)
52         {
53             cin >> a >> b >> rab >> cab >> rba >> cba;
54             e[tot++] = money(a, b, rab, cab);
55             e[tot++] = money(b, a, rba, cba);
56         }
57         if(bellman(s, v))cout<<"YES"<<endl;
58         else cout<<"NO"<<endl;
59     }
60     return 0;
61 }

原文地址:https://www.cnblogs.com/fzl194/p/8728744.html

时间: 2024-08-30 02:46:40

POJ-1860 Currency Exchange---Bellman-Ford判断正环的相关文章

POJ #1860 Currency Exchange 最短路径算法 判断负环

Description 题目描述在这里:链接 更多的样例:链接 思路 我们把每种货币看成图的顶点,而每个交换站点实现一对货币的交换,可认为增加一个交换站点就是增加两个顶点间的一个环路.从样例中可以知道,如果想要让NICK的资金增加,那么就需要存在一个权值为正的环路,使得货币总价值能够无限上升. 所以现在的问题变成了如何判断图中是否有正环.由于货币交换后总价值可能降低,也就是说可能存在负权边,那么 Dijkstra 就不适用了,应该采用能判断负环的 Bellman_ford ,还有它的优化算法 S

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

链接:poj 1860 题意:给定n中货币,以及它们之间的税率,A货币转化为B货币的公式为 B=(V-Cab)*Rab,其中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会增加 分析:这个题就是判断是否存在正权回路,可以用bellman-ford算法,不过松弛条件相反 也可以用SPFA算法,判断经过转换后,转换为原本货币的值是否比原值大... bellman-ford    0MS #include<stdio.h> #include<string.h> str

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

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 7114 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

poj 1860 Currency Exchange 解题报告

题目链接:http://poj.org/problem?id=1860 题目意思:给出 N 种 currency, M种兑换方式,Nick 拥有的的currency 编号S 以及他的具体的currency(V).M 种兑换方式中每种用6个数描述: A, B, Rab, Cab, Rba, Cba.其中,Rab: 货币A 兑换 货币B 的汇率为Rab,佣金为Cab.Rba:货币B 兑换 货币 A 的汇率,佣金为Cba.假设含有的A货币是x,那么如果兑换成B,得到的货币B 就是:(x-Cab) *

[2016-04-13][POJ][1860][Currency Exchange]

时间:2016-04-13 23:48:46 星期三 题目编号:[2016-04-13][POJ][1860][Currency Exchange] 题目大意:货币对换,问最后能否通过对换的方式使钱变多, 分析: 直接spfa判断是否存在环,如果存在那么就能无限增值 如果不存在正环,那么直接判断最终d[s] 是否 大于初始值 #include<cstdio> #include<vector> #include<cstring> #include<queue>

poj 1860 Currency Exchange(Bellman-Ford 改)

poj 1860 Currency Exchange Description 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

Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange

/* 求解单源最短路问题:Dijkstra算法(该图所有边的权值非负) 关键(贪心): (1)找到最短距离已经确定的节点,从它出发更新与其相邻节点的最短距离: (2)此后不再关心(1)中“最短距离已经确定的节点”. 时间复杂度(大概的分析,不准确): “找到最短距离已经确定的节点” => O(|V|) "从它出发更新与其相邻节点的最短距离" => 邻接矩阵:O(|V|),邻接表:O(|E|) 需要循环以上两个步骤V次,所以时间复杂度:O(V^2) 即:在|E|较小的情况下,

poj 1860 Currency Exchange(SPFA)

题目链接:http://poj.org/problem?id=1860 Description 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 b

最短路(Bellman_Ford) POJ 1860 Currency Exchange

题目传送门 1 /* 2 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 3 详细解释:http://blog.csdn.net/lyy289065406/article/details/6645778 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <vector>