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. CentOS 6.4的安装--史上最全-CRPER木木

    安装工具及软件: VmwareWorkstation9 CentOS-6.4-x86_64-LiveCD Vmware初始设置:           刚装好的VMWARE启动后,虽说默认已经设置好基础 ...

  2. Linux入门之——安装虚拟机软件

    /** ****************************************************************************** * @author    暴走的小 ...

  3. Vijos P1325桐桐的糖果计划(有向图双连通分量)

    /*重边不能删 不能删 不能删...*/ #include<iostream> #include<cstdio> #include<cstring> #define ...

  4. 如何做高大上的网站布局 -------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )

    SEO开始做,最重要的是网站布局,一个网站布局决定了用户在网站的停留时间,在网站中放入用户想要的内容之外,更重要的是要让用户看到网站之后,一目了然,视觉和感官上良好的体验.那新手该如何做网站布局呢? ...

  5. Mysql 中和同to_char 一样用法的函数

    STR_TO_DATE() $sql = " SELECT "; $sql .= " m_img,m_content,STR_TO_DATE(m_time,\" ...

  6. java中?和A、B、T、E、K、V的区别

    使用泛型 List<T> list = new ArrayList<T>(); T t = list.get(0); 不使用泛型 List list = new ArrayLi ...

  7. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

  8. Hive学习之二 《Hive的安装之自定义mysql数据库》

    由于MySQL便于管理,在学习过程中,我选择MySQL. 一,配置元数据库. 1.安装MySQL,采用yum方式. ①yum  install  mysql-server,安装mysql服务端,安装服 ...

  9. 基于GBT28181:SIP协议组件开发-----------第二篇SIP组件开发原理

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3937590.html,qq:1269122125. 上一节中讲的S ...

  10. int, NSInteger, NSUInteger, NSNumber的区别

    新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...