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. 使用Tcl脚本分配FPGA管脚

    自己主动生成Tcl文件 Project -> Generate Tcl File for Project... 弹出例如以下对话框.设置脚本路径. 编辑引脚 使用set_location_ass ...

  2. Effective C++ 总结(二)

    四.设计与声明          条款18:让接口容易被正确使用,不易被误用      理想上,如果客户企图使用某个接口而却没有获得他所预期的行为,这个代码不该通过编译:如果代码通过了编译,它的行为就 ...

  3. svcutil 生成代理类时的问题

    如果有这个的xsd, group内嵌choice的结构: <xs:complexType name="CreateType">        <xs:sequen ...

  4. Python之路,Day4 - Python基础4 (new版)

    Python之路,Day4 - Python基础4 (new版)   本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...

  5. 转载:C# 之泛型详解

    本文原地址:http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html.感谢博主分享! 什么是泛型 我们在编写程序时,经常遇到两个模 ...

  6. HTML (1)href与Action,get post

    1.   href与Action的区别 href只能get参数,action能get参数又能post参数 href一般用于单个连接,可以带参数(URL重写),是采用get方式请求的,在地址栏中可以看到 ...

  7. input输入过滤js

    html部分使用方式 <input  onkeyup="usrNameSet(this)" /> 其它的自己可以随便调用 Js部分 //只能输入数字.字母.小数点.汉字 ...

  8. memcache的安装及管理

    一.Memcache概述 Memcache(内存,缓存):是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个巨大的hash表.(key=value)(是用C语言开发的,并且需要libeven ...

  9. ASP.NET实现文件下载

    转:http://blog.csdn.net/codeshark/article/details/2473664 方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流, ...

  10. (转)dedecms入门

    学dedecms一段时间了,把我的入门体会和大家分享一下. 什么是dedecm cms(内容管理系统):现在有各种内容模型,如书评(包括书名,出版社,评论等字段).cms一般有用户后台,网页的用户可以 ...