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 ...
随机推荐
- C#检测系统是否激活[转自StackOverFlow]
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...
- EIP-uni-app框架-建立项目
uni-app介绍 uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架,开发者编写一套代码,可编译到iOS.Android.H5.小程序等多个平台.uni-app在跨端数量,扩展能力, ...
- 网易CentOS yum源
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
- beijing(数学题)
beijing(数学题) 甲和乙随机进行2n+1场n胜球赛,赌球必须对每场球赛单独押注.由于小明是甲队的铁杆球迷,现在小明希望如果甲最终获胜,那么他获得\(2^{2n-1}\)元,否则乙队获胜,他失去 ...
- Python flask虚拟环境安装
1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在 ...
- cuda测试二维block的使用
#include "cuda_runtime.h" #include <stdio.h> #include <stdlib.h> #include < ...
- HTTP基本认证和JWT鉴权
一.HTTP基本认证 Basic Authentication——当浏览器访问使用基本认证的网站的时候, 浏览器会提示你输入用户名和密码. http auth的过程: · 客户端发送http请求 · ...
- LinkedList实现原理
原文链接:https://www.jianshu.com/p/56c77c517e71 本文对LinkedList的实现讨论都基于JDK8版本 Java中的LinkedList类实现了List接口和D ...
- 查看ip常见命令...
1.获取ip Unix用户可以在命令提示符中输入ifconfig来获取. 使用Windows的用户,请尝试使用 ipconfig 命令.
- 资深专家深度剖析Kubernetes API Server第2章(共3章)
欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...