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
题意:有n种货币,有m个兑换点,每个兑换点可以在收取Cab(币种为A)佣金(佣金固定!)后将A按Rab汇率换成B,也可在收取Cba(币种为B)佣金后将B按Rab汇率换成A,你现在有币种为S(我就不说S币了)的货币V元
请问你是否可以通过某些兑换将S换成其他货币,再换回S以获得更多的钱?
题解:很明显的一道spfa求正环,bfs方法中只需要一个点进队超过n次即构成正环
求正环吗,自然求的是最长路,而且d[x]+w>y也要改成d[x]*w>y,这也是常识了
代码如下:
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; struct node
{
int x;
double r,c;
}; vector<node> g[];
double d[],c;
int vis[],cnt[];
int n,m,s; int check(int u)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
d[i]=;
}
d[u]=c;
queue<int> q;
q.push(u);
while(!q.empty())
{
int x=q.front();
int sz=g[x].size();
vis[x]=;
q.pop();
for(int i=;i<sz;i++)
{
int y=g[x][i].x;
double r=g[x][i].r,c=g[x][i].c;
if((d[x]-c)*r>d[y])
{
d[y]=(d[x]-c)*r;
cnt[y]++;
if(!vis[y])
{
q.push(y);
vis[y]=;
if(cnt[y]==n)
{
return ;
}
}
}
}
}
return ;
} int main()
{
scanf("%d%d%d%lf",&n,&m,&s,&c);
for(int i=;i<=m;i++)
{
int f,t;
double rab,cab,rba,cba,c1,c2;
scanf("%d%d%lf%lf%lf%lf",&f,&t,&rab,&cab,&rba,&cba);
node hehe;
hehe.x=t;
hehe.r=rab;
hehe.c=cab;
g[f].push_back(hehe);
hehe.x=f;
hehe.r=rba;
hehe.c=cba;
g[t].push_back(hehe);
}
int ans=check(s);
if(ans)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}


poj1860 Currency Exchange(spfa判断正环)的更多相关文章

  1. POJ1860 Currency Exchange —— spfa求正环

    题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

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

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

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

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

  4. POJ1680 Currency Exchange SPFA判正环

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

  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. poj3621 SPFA判断正环+二分答案

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

  8. HDU 1317(Floyd判断连通性+spfa判断正环)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

随机推荐

  1. 有关Botton的用法(一)

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...

  2. Delphi从Internet下载文件

    Delphi从Internet下载文件   今天在做拍卖系统的时候,因考虑到网络状况问题,需要将拍品所有信息下载到本机,包括拍品图片,因此需要实现从Internet下载文件的功能.      下面是代 ...

  3. 处理mysql主从中断

    主从同步中断跳过处理步骤: slave stop;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;slave start; 在使用set  global sql_slave_s ...

  4. laravel前后端分离的用户登陆 退出 中间件的接口与session的使用

    在项目开发的过程中,需要有用户的登陆 退出 还有校验用户是否登陆的中间件; 基本思路: 登陆: 前端请求接口的参数校验 用户名 密码规则的校验 用户名密码是否正确的校验; 如果上面的校验都通过的了,把 ...

  5. Studio 3T 如何使用 Query Builder 查询数据

    Studio 3T 是一款对 MongoDB 进行数据操作的可视化工具. 在 Studio 3T 中,我们可以借助 Query Builder 的 Drag & Drop 来构建查询条件. 具 ...

  6. Java-Runoob:Java 开发环境配置

    ylbtech-Java-Runoob:Java 开发环境配置 1.返回顶部 1. Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Lin ...

  7. 1097 Deduplication on a Linked List

    题意: 给出一个链表,删除绝对值相同的结点,对于每个绝对值为K的结点,只保留第一次出现的那个.把被移除的结点组成一个新链表,输出删除去重后的链表和新链表. 思路:考察链表的“删除”操作,不难. 代码: ...

  8. 转载:细说oracle 11g rac 的ip地址

    本文转载自:细说oracle 11g rac 的ip地址 http://blog.sina.com.cn/s/blog_4fe6d4250102v5fa.html 以前搭建oracle rac的时候( ...

  9. StampedLock

    StampedLock是Java8引入的一种新的所机制,简单的理解,可以认为它是读写锁的一个改进版本,读写锁虽然分离了读和写的功能,使得读与读之间可以完全并发,但是读和写之间依然是冲突的,读锁会完全阻 ...

  10. Java面试(4)

    1 哈希函数满足什么条件? 计算简单/散列地址分布均匀. 2 hash处理冲突方式?HashMap采用的什么办法? 开放地址法: 线性探测 . 线性补偿法.伪随机数法. 拉链法:HashMap采用链地 ...