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. 技术型创业者easy遇到的三大问题

    关于创业这事儿.由于经历的事情实在是太多了,所以真是想到哪儿写到哪儿. 这一篇算是<杂记>的番外篇.我今天想说的东西太多了,实在是非常想和很多其它人讨论讨论关于创业的事情. 这样的感觉就像 ...

  2. [Javascript] Log Levels and Semantic Methods

    Go beyond console.log by learning about log levels, filtering log output and structuring your output ...

  3. golang中channel的超时处理

    并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...

  4. SPOJ 345 - Mixtures 区间动态规划

    有n个混合物排成一排,每个混合物有一个颜色值0<=color<=99, 规定合并只能合并相邻两个, 将颜色a的混合物与颜色b的混合物合并后,颜色为( a+b ) % 100,并产生a*b的 ...

  5. python手机号码运营商归属测试

    #手机号码测试: def number_test(): while True: number = input('Please enter your phone number:') CN_mobile ...

  6. shijan

    1.<?php 2. $zero1=date(“y-m-d h:i:s”); 3. $zero2=”2010-11-29 21:07:00′; 4. echo “zero1的时间为:”.$zer ...

  7. git针对Android Studio的使用

    1.将文件放到项目根目录下 .gitignore 文件内容: *.iml.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_ ...

  8. Oracle oerr使用

    [oracle@cuug ~]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number %s wit ...

  9. 想加入一行代码吗?使用<code>标签

    在介绍语言技术的网站中,避免不了在网页中显示一些计算机专业的编程代码,当代码为一行代码时,你就可以使用<code>标签了,如下面例子: <code>var i=i+300;&l ...

  10. 六、C# 派生

    派生 对一个现有的类型进行扩展,以便添加更多的功能,或者对现有的类型的操作进行重写.   比如可以将两个类都适用的方法和属性,用一个新的类进行重构,两个类再分别继承这个类.   定义一个派生类时,要在 ...