POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
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, CAB, 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
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种货币交换关系,交换率和手续费,给你起始的货币类型和金额,问你是否可以通过交换货币,最后回到起始的货币时能盈利。 解题思路:如果要盈利,只需要判断图中存不存在正环, 即可以一直让某种货币额度无限增加。由于是无向图,那么只要存在正环,那么我就可以最后转化成起始的货币且盈利。所以只要将SPFA判负环的条件变化一下就行。初始值时,让除原点之外的d数组都赋值为0。同时松弛条件变为d[e.to] < (d[e.from] - e.com)*e.rate。即可,最后判断当u为起点时的d[u]是否大于起始金额即可。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+200;
int n , m;
struct Edge{
int from,to;
double rate , com;
};
vector<Edge>edges;
vector<int>G[maxn];
void init(){
for(int i = 0; i <= n; i++){
G[i].clear();
}
edges.clear();
}
double d[maxn] ,cnt[maxn], inq[maxn];
void AddEdge(int u,int v,double r,double co){
edges.push_back( (Edge){u,v,r,co} );
m = edges.size();
G[u].push_back(m-1);
} bool SPFA(int s, double V){
queue<int>Q;
for(int i = 0; i <= n; i++){
d[i] = 0;
}
d[s] = V;
cnt[s] ++;
inq[s] = 1;
Q.push(s);
while(!Q.empty()){
int u = Q.front();
Q.pop();
if(u == s&& d[s] > V){
return true;
}
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++){
Edge & e = edges[G[u][i]];
if(d[e.to] < (d[e.from] - e.com)*e.rate ){
d[e.to ] = (d[e.from] - e.com) *e.rate;
if(!inq[e.to]){
inq[e.to] = 1;
Q.push(e.to);
}
}
}
}
return false;
}
int main(){
int mm,s;
double k;
while(scanf("%d%d%d%lf",&n,&mm,&s,&k)!=EOF){
int a,b;
double c,d;
for(int i = 0; i < mm; i++){
scanf("%d%d%lf%lf",&a,&b,&c,&d);
AddEdge(a,b,c,d);
scanf("%lf%lf",&c,&d);
AddEdge(b,a,c,d);
}
bool yes = SPFA(s,k);
if(yes){
puts("YES");
}else{
puts("NO");
}
}
return 0;
}
POJ 1860——Currency Exchange——————【最短路、SPFA判正环】的更多相关文章
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- poj 1860 Currency Exchange (最短路bellman_ford思想找正权环 最长路)
感觉最短路好神奇呀,刚开始我都 没想到用最短路 题目:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 mo ...
- POJ 1860 Currency Exchange 最短路 难度:0
http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...
- 最短路(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 ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- .NET 生成生成缩略图
/// <summary> /// 生成缩略图 /// </summary> /// <param name="FromImagePath">源 ...
- 解决VMware Workstation 不可恢复错误: (vcpu-0)
转载:http://tieba.baidu.com/p/3487673152 如图的错误 如果你按照破解了mac支持的VMware Workstation 11的新建虚拟机向导一步一步创建了一个mac ...
- ConcurrentHashMap原理详解
参考链接:https://www.cnblogs.com/chengxiao/p/6842045.html https://www.cnblogs.com/ITtangtang/p/3948786.h ...
- P2597 [ZJOI2012]灾难
\(\color{#0066ff}{ 题目描述 }\) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝, ...
- P1959 遗址_NOI导刊2009普及(6)
题意:平面上n个点(坐标$0\le x,y\le 5000,n \le 3000$) 求以其中四个点为顶点的正方形的最大面积 $O(n^2)$枚举两个点作为当前正方形的对角线 那么如何求出另外两个点呢 ...
- 状压DP 【洛谷P3694】 邦邦的大合唱站队
[洛谷P3694] 邦邦的大合唱站队 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...
- Oracle表的优化一点见解
Oracle优化的几个简单步骤 数据库优化的讨论可以说是一个永恒的主题.资深的Oracle优化人员通常会要求提出性能问题的人对数据库做一个statspack,贴出数据库配置等等.还有的人认为要抓出执行 ...
- linux进程池模型
static int nchildren;static pid_t* pids;int main(int argc,char**argv){ int listenfd,i; socklen_t add ...
- 1. mybatis 的入门实例
mybatis 的入门实例 1.创建一个普通的Java项目 1.加入jar包(所有mybatis 和mysql) 2.加入配置文件 src 目录下 (1) db.properties mysql.us ...
- git 日常使用从入门到真香
目录 git 日常使用从入门到真香 一.Git简介 二.Git常用命令 三.git操作流程 四.报错处理 git 日常使用从入门到真香 一.Git简介 Git是一个开源的分布式版本控制系统,可以有效. ...