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 R AB, C AB, R BA and C BA - 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<=10 3.  For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.  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 10 4

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

题目意思:有n种货币,货币之间按照汇率交换,当然还要花费一些手续费,货币交换是可以多次重复进行的,问有没有可能经过一系列的货币交换,开始的货币会增加?
当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。
解题思路:这道题可以抽象为图论中的题,将货币种类看为点,货币之间的交换看为有向边,想要货币的金额产生增加,那么必然要有正权回路,即在一条回路上能够一直松弛下去。该题的问题主要在于所给的参数很多,第一行给出了n种货币有m种交换方式,给你第s种货币有V的金额,对于m种的交换方式,从x到y需要汇率rate和手续费commission,从y到x也需要这两个参数。同时这里的松弛递推公式也要发生变化:
            if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
{
dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
}
因为是需要增加的正权回路,所以如果小于就松弛。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct Edge
{
int f;
int t;
double r;
double c;
} edge[];
double dist[];
int n,m,s,cnt;
double x;
int bellman_ford()
{
int i,j;
int flag;
for(i=; i<=n; i++)
{
dist[i]=;
}
dist[s]=x;
for(j=; j<=n; j++)
{
flag=;
for(i=; i<=cnt; i++)
{
if(dist[edge[i].t]<(dist[edge[i].f]-edge[i].c)*edge[i].r)
{
dist[edge[i].t]=(dist[edge[i].f]-edge[i].c)*edge[i].r;
flag=;
}
}
if(flag==)
{
break;
}
}
return flag;
}
int main()
{
int i,t;
int u,v;
double a1,a2,b1,b2;
while(scanf("%d%d%d%lf",&n,&m,&s,&x)!=EOF)
{
cnt=;
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&u,&v,&a1,&b1,&a2,&b2);
edge[cnt].f=u;
edge[cnt].t=v;
edge[cnt].r=a1;
edge[cnt++].c=b1;
edge[cnt].f=v;
edge[cnt].t=u;
edge[cnt].r=a2;
edge[cnt++].c=b2;
}
if(bellman_ford())
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}

附上使用SPFA的代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxs = 1e3+;
int n,m;
struct Edge
{
int to;
double rate;
double com;
} ;
double dis[maxs];
int vis[maxs];
int cnt[maxs];///用来记录入队列次数
vector<Edge>maps[maxs];
void AddEdge(int u,int v,double r,double co)
{
Edge t;
t.to=v;
t.rate=r;
t.com=co;
maps[u].push_back(t);
}
int SPFA(int s, double v)
{
int i;
memset(dis,,sizeof());
memset(vis,,sizeof());
memset(cnt,,sizeof());
queue<int>q;
dis[s]=v;
vis[s]=;
cnt[s]++;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(i=; i<maps[u].size(); i++)
{
int to=maps[u][i].to;
double com=maps[u][i].com;
double rate=maps[u][i].rate;
if(dis[to]<(dis[u]-com)*rate)
{
dis[to]=(dis[u]-com)*rate;
if(!vis[to])
{
vis[to]=;
cnt[to]++;
if(cnt[to]>=n)
{
return ;
}
q.push(to);
}
}
}
}
return ;
}
int main()
{
int s,i;
double k;
while(scanf("%d%d%d%lf",&n,&m,&s,&k)!=EOF)
{
int a,b;
double c,d,e,f;
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
AddEdge(a,b,c,d);
AddEdge(b,a,e,f);
}
if(SPFA(s,k))
{
puts("YES");
}
else
{
puts("NO");
}
}
return ;
}


Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路的更多相关文章

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

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

  2. Currency Exchange POJ - 1860 (spfa判断正环)

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

  3. POJ1680 Currency Exchange SPFA判正环

    转载来源:優YoU  http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...

  4. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  5. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  6. POJ1860-Currency Exchange (正权回路)【Bellman-Ford】

    <题目链接> <转载于 >>> > 题目大意: 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0. ...

  7. HDU - 1317 ~ SPFA正权回路的判断

    题意:有最多一百个房间,房间之间连通,到达另一个房间会消耗能量值或者增加能量值,求是否能从一号房间到达n号房间. 看数据,有定5个房间,下面有5行,第 iii 行代表 iii 号 房间的信息,第一个数 ...

  8. Bellman_ford货币兑换——正权回路判断

    POJ1860 题目大意:你在某一点有一些钱,给定你两点之间钱得兑换规则,问你有没有办法使你手里的钱增多.就是想看看转一圈我的钱能不能增多,出现这一点得条件就是有兑换钱得正权回路,所以选择用bellm ...

  9. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

随机推荐

  1. CCF 201709-2公共钥匙盒

    问题描述 有一个学校的老师共用N个教室,按照规定,所有的钥匙都必须放在公共钥匙盒里,老师不能带钥匙回家.每次老师上课前,都从公共钥匙盒里找到自己上课的教室的钥匙去开门,上完课后,再将钥匙放回到钥匙盒中 ...

  2. java面向对象之个人总结

    面向对象有三大特性:继承,封装,多态 1.继承: (1)继承的特点:A,java支持单根继承,不支持多根继承 B,java支持多层继承(继承体系) (2)细节注意:A.子类只能继承父类的非私有成员(成 ...

  3. Linux-2.6_LCD驱动学习

    内核自带的驱动LCD,drivers/video/Fbmem.c LCD驱动程序 假设app: open("/dev/fb0", ...) 主设备号: 29, 次设备号: 0--- ...

  4. 基于R语言的结构方程:lavaan简明教程 [中文翻译版]

    lavaan简明教程 [中文翻译版] 译者注:此文档原作者为比利时Ghent大学的Yves Rosseel博士,lavaan亦为其开发,完全开源.免费.我在学习的时候顺手翻译了一下,向Yves的开源精 ...

  5. R语言学习笔记(二十一):字符串处理中的元字符(代码展示)

    元字符有自己的特殊含义 [ ]内的任意字符将被匹配 grep(pattern = "[wW]", x = states, value = T) grep(pattern = &qu ...

  6. 20155306 白皎 《网络攻防》Exp1 PC平台逆向破解——逆向与Bof基础

    20155306 白皎 <网络攻防>Exp1 PC平台逆向破解--逆向与Bof基础 实践相关说明 1.1 实践目标 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. ...

  7. My97DatePicker:开始时间和结束时间的最大间隔为1个月30天,并且不大于当前时间(3种方法)

    问题的背景 在之前做Web项目的时候,开始时间和结束时间,只有2个要求: 1.开始时间必须小于等于结束时间,不能超过当前时间. 2.结束时间必须大于等于开始时间,不能超过当前时间. 由于开始时间不大于 ...

  8. [BZOJ3772]精神污染 主席树上树+欧拉序

    3772: 精神污染 Time Limit: 10 Sec  Memory Limit: 64 MB Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位 ...

  9. 柯朗微积分与数学分析习题选解(1.1 节 a)

    一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...

  10. HBase数据模型的一些概念

    首先来先理解一个概念:HBase是一种列式存储的分布式数据库. 表              在HBase中数据以表的形式存储.使用表的主要原因是把某些列组织起来一起访问,同一个表中的数据通常是相关的 ...