题目链接:http://poj.org/problem?id=1860

Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 32128   Accepted: 12228

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 
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 RAB, CAB, RBA and CBA - 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

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102
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 104

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

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

Source

Northeastern Europe 2001, Northern Subregion
 
 
 
题解:
当能够最终换回初始货币,并且获利时,满足以下两种情况之一:
1.用spfa求出了正环,并且初始点在环内。此种情况理所当然地符合条件。
2.用spfa求出了正环,然而初始点不在环内。那么:可以在这个环内兜转无数次,赚了无数钱,然后再花费一定的代价换回到初始货币(不管代价多大,由于赚的钱是无限的,所以最终还是会获利)。
综上:只要能求出正环,就能获利。
 
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m,X;
double V; struct edge
{
double r, c;
int to,next;
}edge[MAXN*MAXN];
int cnt, head[MAXN]; void addedge(int u, int v, double r, double c)
{
edge[cnt].to = v;
edge[cnt].r = r;
edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} double dis[MAXN];
int times[MAXN], inq[MAXN];
bool spfa(int st)
{
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = -INF; queue<int>Q;
Q.push(st);
inq[st] = ;
dis[st] = V;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v]< (dis[u]-edge[i].c)*edge[i].r )
{
dis[v] = (dis[u]-edge[i].c)*edge[i].r;
if(!inq[v])
{
Q.push(v);
inq[v] = ;
if(++times[v]>n) return true;
}
}
}
}
return false;
} int main()
{
while(scanf("%d%d%d%lf",&n,&m,&X, &V)!=EOF)
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
double r1,c1,r2,c2;
scanf("%d%d%lf%lf%lf%lf", &u, &v, &r1,&c1,&r2,&c2);
addedge(u,v,r1,c1);
addedge(v,u,r2,c2);
}
if(spfa(X))
puts("YES");
else
puts("NO");
}
}

POJ1860 Currency Exchange —— spfa求正环的更多相关文章

  1. poj1860 Currency Exchange(spfa判断正环)

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  2. POJ1680 Currency Exchange SPFA判正环

    转载来源:優YoU  http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...

  3. poj1860 Currency Exchange(spfa判断是否存在正环)

    题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...

  4. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  5. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  6. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  7. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  8. poj3621 SPFA判断正环+二分答案

    Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...

  9. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. centos7 下修改网络配置

    修改ip地址 编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static 静态ip DEFROUTE=yes ...

  2. 洛谷 [P3480] KAM-Pebbles

    博弈论转化 本题的限制条件很多,我们尝试转化, 我们发现,定义 c[i] 为第 i 堆可以取得数量,如果第 i 堆取出了 x ,那么 c[i] - x , c[i + 1] + x 我们发现这是一个反 ...

  3. python生成器及迭代器

    一.迭代器 迭代器是访问集合元素的一种方式 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 迭代器有两个基本的方法: ...

  4. 转自CSDN,关于状态机

    有限状态机FSM思想广泛应用于硬件控制电路设计,也是软件上常用的一种处理方法(软件上称为FMM--有限消息机).它把 复杂的控制逻辑分解成有限个稳定状态,在每个状态上判断事件,变连续处理为离散数字处理 ...

  5. UIApplicationDelegate详解

    
每 个iPhone应用程序都有一个UIApplication,UIApplication是iPhone应用程序的开始并且负责初始化并显示 UIWindow,并负责加载应用程序的第一个UIView到U ...

  6. 属性font-family:Font property font-family does not have generic default

    以前定义字体都是用的常用的字体,也没注意过会有这个提示,昨天在写界面的时候重新定义了一个本地没有的字体,发现会有提示. W3C的文档: font-family:<family-name>, ...

  7. 深入爬虫书scrapy 之json内容没有写入文本

    settings.py设置 ITEM_PIPELINES = { 'tets.pipelines.TetsPipeline': 300, } spider代码 xpath后缀添加.extract() ...

  8. ACM用到的算法。先做个笔记,记一下

    ACM 所有算法 数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 ...

  9. 电音中DJ/Producer/MC/EDM/Remix/Mix的名词解释(转)

    DJ DJ是Disc Jockey的缩写,是电音圈子里的一种热门职业,一般大家在夜店或者酒吧看到的站在台上甩着膀子拧着按钮或者使劲儿搓碟的就是DJ啦. DJ的主要工作一般就是在现场用打碟机和混音台把许 ...

  10. 最新的hustoj搭建姿势

    试着照某度上的教程搭了一下hustoj,出了一些问题,之前的搭建姿势很多已经不适用了,重新整理一下思路,方法二简单粗暴: 方法一: 首先虚拟机安装了Elementory OS (基于Ubuntu的衍生 ...