POJ 1860 Currency Exchange【SPFA判环】
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
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条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式为(钱数-税)*汇率,问最后钱的数目是否会增多
【分析】
建图,一种货币就是一个点,货币交换作为有向边。边的权值需要小心,A到B的权值为(V(A) - C)*R。看到正权回路,应该想到负权回路,那思考一下是不是能用Bellman-Ford来做呢,其实这个问题刚刚好是相反的,这里需要求最长路,那么把dist初始化为0,dist[s]=v,松弛条件相反,利用Bellman-Ford的思想就能解决这道题了。
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5+;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int tot,n,m,x,s;
int u,w;
double v;
double ab1,ab2,ba1,ba2;
double dis[maxn];
int cnt[maxn],vis[maxn];
struct cmp
{
bool operator()(int a,int b)
{
return dis[a] > dis[b];
}
}; int head[maxn];
struct node
{
int v,nxt;
double r,c,w;
}e[maxn];
void init()
{
tot=;
ms(head,-);
ms(dis,);//求最长路径开始设为0
ms(vis,);
}
void add(int u,int v,double r,double c)
{
e[tot].r=r;
e[tot].c=c;
e[tot].v=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} int spfa(int s)
{
queue<int> q;
dis[s]=v;
vis[s]=;
cnt[s]++;
q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
vis[u]=; //
for(int i=head[u];~i;i=e[i].nxt)
{
int v = e[i].v;
//本金 减去利息 再乘汇率;
e[i].w = (dis[u] - e[i].c)*e[i].r-dis[u];
if(dis[v] < dis[u] + e[i].w)
{
dis[v] = dis[u] + e[i].w;
if(!vis[v])//防止出现环,也就是进队列重复了
{
vis[v]=;
q.push(v);
//如果一个点能变大n次以上说明还能继续增大&说明原值已经可以通过转换增大
if(++cnt[v]>n) return -;//有负环
}
}
}
}
return ;
} int main()
{
while(~scanf("%d%d%d%lf",&n,&m,&s,&v))
{
init();
int a,b; for(int i=;i<=m;i++)
{
scanf("%d%d%lf%lf",&a,&b,&ab1,&ab2);
add(a,b,ab1,ab2);//双向链表
scanf("%lf%lf",&ba1,&ba2);
add(b,a,ba1,ba2);
}
if(spfa(s)==-) puts("YES");
else puts("NO");
}
}
/*
【题意】 【类型】
SPFA判断负环变形
【分析】 【时间复杂度&&优化】 【trick】 【数据】
*/
SPFA判环
POJ 1860 Currency Exchange【SPFA判环】的更多相关文章
- POJ 1860 Currency Exchange (SPFA松弛)
题目链接:http://poj.org/problem?id=1860 题意是给你n种货币,下面m种交换的方式,拥有第s种货币V元.问你最后经过任意转换可不可能有升值.下面给你货币u和货币v,r1是u ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
- (简单) POJ 1860 Currency Exchange,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 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- js的数据类型--字符串
js的数据类型——字符串 这篇我们来说说js的第二种数据类型——字符串. js的内置功能之一就是字符串拼接.如果将加号(+)运算符用于数字,表示两数相加.但将它作用于字符串,则表示字符串拼接,将第二个 ...
- 【Contest Hunter【弱省胡策】Round #0-Flower Dance】组合数学+DP
题目链接: http://ch.ezoj.tk/contest/%E3%80%90%E5%BC%B1%E7%9C%81%E8%83%A1%E7%AD%96%E3%80%91Round%20%230/F ...
- (转)matlab练习程序(HOG方向梯度直方图)
matlab练习程序(HOG方向梯度直方图)http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html HOG(Histogram o ...
- Spring总结以及在面试中的一些问题(山东数漫江湖)
1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...
- 大聊Python----通过Socket实现简单的ssh客户端
光只是简单的发消息.收消息没意思,干点正事,可以做一个极简版的ssh,就是客户端连接上服务器后,让服务器执行命令,并返回结果给客户端. #ssh_client.py import socket cli ...
- VMware12序列号
VMware tools怎么删除 rpm -e open-vm-tools-desktop vm12序列号 5A02H-AU243-TZJ49-GTC7K-3C61NVF5XA-FNDDJ-085GZ ...
- LCD实验学习笔记(六):存储控制器
s3c2440可使用地址空间为1GB(0x00000000到0x40000000). 1G空间分为8个BANK,每个BANK为128MB. 设27条地址线,和8个片选引脚(nGCS0-nGCS7). ...
- mysql中的单引号/小数点/字符转换为数字/警告信息
我们准备玩点有趣的: select 一个数字: mysql from mysql.user; +---+ | +---+ | | | +---+ rows in set (0.00 sec) mysq ...
- mssql手工注入1
强制字符转成数字, 所以报错, 能获得数据 查版本号: http: -- 查数据库版本: http: -- 查当前数据库用户(如果看到dbo 那么多半当前数据库的用户是dba权限): http: -- ...
- php中的__call()函数重载
<?php #调用类中没有的方法时, 会自动调用__call方法重载 #第一个参数是调用时的方法名, 第二个参数为参数组成的数组 class Cat{ public function Hello ...