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 ...
随机推荐
- 2015/8/30 Python基础(4):序列操作符
序列是指成员有序排列,可以通过下标偏移量访问的类型.Python序列包括:字符串.列表和元组.序列的每个元素可以指定一个偏移量得到,多个元素是通过切片操作得到的.下标偏移量从0开始计数到总数-1结束. ...
- Linux和windows下检查jsp后门文件的方法
Linux下: find . -name "*.jsp" | xargs egrep -liw "createNewFile| File\(| File |applica ...
- idea 修改静态资源不需要重启的办法
快捷键Ctrl + Alt + S打开设置面板,勾选Build project automatically选项: 快捷键Ctrl + Shift + A查找registry命令: 在查找到的regis ...
- 【Dream Counting, 2006 Dec-数数的梦】数位dp
题意:给定两个数,问区间[A,B]中0~9分别出现了多少次.A,B<=10^18 题解:应该是最裸的数位dp吧..一开始没有记忆化tle了TAT 我们可以求出区间[0,B]的,再减去区间[0,A ...
- 打印菱形(c语言)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { // 定 ...
- B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- php中比较好用的函数
PHP中一个好用的函数parse_url,特别方便用来做信息抓取的分析,举例子如下: $url = "http://www.electrictoolbox.com/php-extract-d ...
- 【Matlab】让Matlab程序发出声音
我有时候运行一段很长的代码,在等待的时候去做别的事,希望程序运行完可以有一个提示音. 这可以用matlab的一个函数sound实现,该函数的输入参量是音频数据向量.采样频率和转换位数. % 响一声 s ...
- gnu app url[web][5星]
http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...
- MVC 从控制器将数据对象赋值给前端JS对象
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...