Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16456   Accepted: 5732

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
 // spfa
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
queue <int> que;
double rate[][];
double commission[][];
double dis[];
int n,m,s;
double v;
int inque[];
int map[][]; bool spfa()
{
while(!que.empty())
que.pop();
memset(inque,,sizeof(inque));
for(int i = ; i <= n; i++)
dis[i] = ;
dis[s] = v;
que.push(s);
inque[s] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ;
for(int i = ; i <= n; i++)
{
if(map[u][i] != INF && dis[i] < (dis[u]-commission[u][i])*rate[u][i])
{
dis[i] = (dis[u]-commission[u][i])*rate[u][i];
if(inque[i] == )
{
inque[i] = ;
que.push(i);
}
}
}
if(dis[s] > v) return true;//若松弛后dis[s] > v 说明有正环
}
return false;
}
int main()
{
int a,b;
while(~scanf("%d %d %d %lf",&n,&m,&s,&v))
{
memset(map,INF,sizeof(map));
for(int i = ; i <= m; i++)
{
scanf("%d %d",&a, &b);
scanf("%lf %lf %lf %lf",&rate[a][b],&commission[a][b],&rate[b][a],&commission[b][a]);
map[a][b] = ;
map[b][a] = ;
}
if(spfa())
printf("YES\n");
else printf("NO\n");
}
return ;
}
 //Bellman_ford

 #include<stdio.h>
#include<string.h>
struct node
{
int u,v;
double rate,comm;
}map[];
int vis[];
int n,m,s,cnt;
double v;
double dis[]; bool Bellman_ford()
{
memset(dis,,sizeof(dis));//dis[]初始化为0;
dis[s] = v;
int i,j,flag;
for(i = ;i <= n; i++)
{
flag = ;
for(j = ; j < cnt; j++)//松弛任意两点
{
if(dis[map[j].v] < (dis[map[j].u]-map[j].comm)*map[j].rate)
{
dis[map[j].v] = (dis[map[j].u]-map[j].comm)*map[j].rate;
flag = ;
}
}
if(flag == )
break;
}
if(i >= n+)//若松弛n次还可以松弛说明存在正环;
return true;
else return false;
}
int main()
{
int a,b;
double c,d,e,f;
while(~scanf("%d %d %d %lf",&n,&m,&s,&v))
{
cnt = ;
while(m--)
{
scanf("%d %d %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f);
map[cnt++] = ((struct node){a,b,c,d});
map[cnt++] = ((struct node){b,a,e,f});
}
if(Bellman_ford())
printf("YES\n");
else printf("NO\n");
}
return ;
}

Currency Exchange(判断是否有正环)的更多相关文章

  1. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

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

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

  3. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

  4. Currency Exchange(SPFA判负环)

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

  5. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  6. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

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

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

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

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

  9. Currency Exchange POJ1860

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

随机推荐

  1. “too many open files" ----增大打开的文件数

     http://www.cnblogs.com/ibook360/archive/2012/05/11/2495405.html [root@localhost ~]# ab -n -c http:/ ...

  2. 浅析mysql 共享表空间与独享表空间以及他们之间的转化

        innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等.对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间.什么是共享表空间和独占表空间共 ...

  3. RT:How HTTP use TCP connection

    In HTTP/0.9 (not used anymore), each request uses a separate TCP connection, and the end of a respon ...

  4. 笔试之Linux命令的使用

    1. awk文本处理工具,显示ps的最后两列 ps -ef|awk '{print $1,$2}' 打印第一和第二域  $0是全域 2. Linux下查看内存使用情况 free

  5. 案例:我行我素购物系统 v1.1

    系统逻辑结构: import java.util.Scanner; public class ShoppingSystem { public static void main(String[] arg ...

  6. 谷歌postman插件用不了的命令行指令

    谷歌postman插件用不了,想测试通过post提交传过来的参数测试,打开所在目录,shift右键,打开命令窗口,输入一下指令,即可获取到提交的值curl -X POST -d 'name=kpf&a ...

  7. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  8. C# Double类型 不四舍五入

    测试中发现Double类型需要#0.00 小数点精度为后2位,并且多余部分不需要四舍五入,直接截断 用字符串处理也可以,但是比较麻烦 这里给出一种思路: double a = 9999.999; a ...

  9. MySql奇葩问题汇总

    当字段名与关键词重叠时,sql语句中用``将字段名括起来,就可解决报错的问题.

  10. PAT - IO-01. 表格输出(5)

    题目: 本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ----------------------------------- ...