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# Winform欢迎窗体实现()
方法一.program.cs 中先启动欢迎窗体,然后注册程序运行空闲去执行主程序窗体相应初始化代码 static void Main(string[] args) { Application.Enab ...
- C# console application executing macro function
C#控制台应用程序,执行或运行Office的宏函数,程序如下: 应用例子:
- 对MySQL性能影响较大的五类配置参数
以下主要是对MySQL 性能影响关系紧密的五大配置参数的介绍. 一. 连接 连接通常来自Web 服务器,下面列出了一些与连接有关的参数,以及该如何设置它们. (一). ...
- JS随机数生成算法
------------------------------------------ 知乎上边淘到的知识,又学到了~ http://www.zhihu.com/question/22818104 -- ...
- Javascript之入门篇(一)
上一篇学习了什么是JavaScript语言及其作用和特有的特点等,本篇将详细介绍JavaScript一些入门使用方式. 对于初学者来讲,由于JavaScript是嵌入到HTML页面里面的,首先创建一张 ...
- P3356 火星探险问题
\(\color{#0066ff}{题目描述}\) 火星探险队的登陆舱将在火星表面着陆,登陆舱内有多部障碍物探测车.登陆舱着陆后,探测车将离开登陆舱向先期到达的传送器方向移动.探测车在移动中还必须采集 ...
- linux下mysql的安装部署
---恢复内容开始--- 注意这一切都是root用户下进行的 su root * 一.查看之前是否安装过:yum list installed mysql* 二.查看是否有安装包:yum list ...
- 【学习笔记】Python 3.6模拟输入并爬取百度前10页密切相关链接
[学习笔记]Python 3.6模拟输入并爬取百度前10页密切相关链接 问题描述 通过模拟网页,实现百度搜索关键词,然后获得网页中链接的文本,与准备的文本进行比较,如果有相似之处则代表相关链接. me ...
- Entity Framework 更新带外键的实体为null
using (var ctx = new PortalContext()){ var city = ctx.Cities.Find(42); ctx.Entry(city) ...
- Xshell添加快捷按钮
1.打开xshell,点击[查看],勾[快速命令]: 2.点击xshell右下角[三],选择[添加按钮],在弹出框的“标签栏”和“文本”栏分别输入名称和命令,最后点击[确定]即可.