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. Android源代码分析之Framework的MediaPlayer

    在Android中MediaPlayer用来播放音频和视频文件,在这里分析下在Framework层中MediaPlayer是怎样调用的.MediaPlayer的代码位于:./frameworks/ba ...

  2. Delpoyment assembly

    遇见异常: java.lang.ClassNotFoundException: 原因:没有对tomcat进行依赖说明 解决: properties - Delpoyment assembly 将tom ...

  3. .Net程序员关于微信公众平台测试账户配置 项目总结

    今天项目第一次验收,夜晚吃过晚饭后,想把项目中用到的关于微信配置总结一下,虽然网上关于这方面的资料很多很多,还有官方API,但是总感觉缺点什么,就像期初做这个项目时,各方面找了很久的资料,说说配置吧! ...

  4. Protocol Buffer Xcode 正确使用思路 成功安装 Xcode7.1

    1. 下载protobuf编译工具 序列化是将数据转换为一个特定的类 http://pan.baidu.com/s/1qWrxHxU 下载解压,它不是用来放在你的项目里 2.打开终端 依次输入并等待指 ...

  5. iOS 格式化输出符号与类型转换

    1.iOS 格式化输出符号 %@    对象 %d,   %i 整数 %u     无符号整形 %f      浮点(双字节) %x,   %X  二进制整数 %o     八进制整数 %zi     ...

  6. Asp.net IsPostBack

    Page.IsPostBack是一个标志:当前请求是否第一次打开.调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者this.Page.IsPos ...

  7. Android JNI 之 环境安装

    在配置环境之前,我们得了解 JNI 和NDK JNI JNI是Java Native Interface的缩写,中文为JAVA本地调用.它提供了若干的API实现了和Java和其他语言的通信(主要是C& ...

  8. 【CEOI2004】锯木厂选址

    [题目描述] 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂.木材只能按照一个方向运输:朝山下运.山脚下有一个锯木厂.另外两个 ...

  9. 【USACO 1.4.4】母亲的牛奶

    [题目描述]  农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原 ...

  10. element的height与width

    关于一个element所有的高度宽度 ele.style.width,ele.style.height:操纵style样式.+"px" offsetWidth.offsetHeig ...