Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 24243   Accepted: 8813

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
题意:有N种货币,M种交换点。将货币a换为货币b时所换到的 货币b价值=(货币a价值-手续费c)*利率r。问给定一种货币S,其价值为V,问是否存在交换方式使货币S交换一圈回来之后其价值变大。
思路:将货币视作结点,交换过程视为路径,利用ford算法,判断图中是否存在无限迭代的环。
/*
1860 Accepted 404K 16MS
*/
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Edge{
int from,to;
double r,c;
}es[MAXN];
int n,E;
bool ford(int s,double v)
{
double d[MAXN];
memset(d,,sizeof(d));
d[s]=v;
while(n--)
{
bool update=false;
for(int i=;i<E;i++)
{
Edge e=es[i];
if(d[e.from]!=&&d[e.to]<(d[e.from]-e.c)*e.r)
{
d[e.to]=(d[e.from]-e.c)*e.r;
update=true;
}
}
if(!update) break;
}
//由ford算法可得:若不存在负环,经过n-1迭代,必能迭代完毕
if(n==-) return true;
return false;
} int main()
{
int N,M,S;
double V;
while(scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF)
{
E=;
n=N;
for(int i=;i<M;i++)
{
int a,b;
double rab,cab,rba,cba;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
es[E].from=a,es[E].to=b,es[E].r=rab,es[E++].c=cab;
es[E].from=b,es[E].to=a,es[E].r=rba,es[E++].c=cba;
} if(ford(S,V)) printf("YES\n");
else printf("NO\n");
} return ;
}

若存在越滚越大的环则财富可以增长。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
double r,c;
}es[MAXN];
struct Node{
int nod;
double captial;
Node(){}
Node(int nod,double captial)
{
this->nod=nod;
this->captial=captial;
}
};
int head[MAXN],tot;
int n,m,src;
double wealth;
double d[MAXN];
int cnt[MAXN];
void addedge(int u,int v,double r,double c)
{
es[tot].to=v;
es[tot].r=r;
es[tot].c=c;
es[tot].net=head[u];
head[u]=tot++;
}
bool spfa()
{
memset(cnt,,sizeof(cnt));
memset(d,,sizeof(d));
d[src]=wealth;
queue<Node> que;
que.push(Node(src,wealth));
while(!que.empty())
{
Node now=que.front();que.pop();
for(int i=head[now.nod];i!=-;i=es[i].net)
{
double money=(now.captial-es[i].c)*es[i].r;
if(money>d[es[i].to])
{
d[es[i].to]=money;
cnt[es[i].to]++;
if(cnt[es[i].to]==n) return true;
que.push(Node(es[i].to,money));
}
}
}
return false;
}
int main()
{
while(scanf("%d%d%d%lf",&n,&m,&src,&wealth)!=EOF)
{
memset(head,-,sizeof(head));
tot=;
for(int i=;i<m;i++)
{
int u,v;
double r1,c1,r2,c2;
scanf("%d%d%lf%lf%lf%lf",&u,&v,&r1,&c1,&r2,&c2);
addedge(u,v,r1,c1);
addedge(v,u,r2,c2);
}
if(spfa())
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}

POJ1860(ford判环)的更多相关文章

  1. POJ3259(ford判环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 39078   Accepted: 14369 Descr ...

  2. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  3. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  4. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  5. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  6. Dwarves (有向图判环)

    Dwarves 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...

  7. COJ 3012 LZJ的问题 (有向图判环)

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...

  8. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  9. E - Andrew and Taxi-二分答案-topo判环

    E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...

随机推荐

  1. Linux 文件系统IO性能优化

    对于LINUX SA来说,服务器性能是需要我们特别关注的,包括CPU.IO.内存等等系统的优化变得至关重要,这里转载一篇非常不错的关于IO优化的文章,供大家参考和学习: 一.关于页面缓存的信息,可以用 ...

  2. 访问一个绝对地址把一个整型数强制转换 (typecast)为一个指针是合法的

    在某工程中,要求设置一绝对地址为0x67a9的整型变量的值为0xaa66.编译器是一个纯粹的ANSI编译器.写代码去完成这一任务. 解析:这一问题测试你是否知道为了访问一个绝对地址把一个整型数强制转换 ...

  3. 九度OJ 1159:坠落的蚂蚁 (模拟、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:277 题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如 ...

  4. combination_m_n

    def combination_2_n(l): n, r = len(l), [] for i in range(0, n, 1): s = i + 1 for ii in range(s, n, 1 ...

  5. ddchuxing——php面试题及答案

    1.  echo和print的区别 echo没有返回值,print有返回值1,执行失败时返回false:echo输出的速度比print快,因为没有返回值:echo可以输出一个或多个字符串,print只 ...

  6. 【题解】P2602[JZOI2010]数字计数

    [题解][P2602ZJOI2010]数字计数 乍看此题,感觉直接从数字的位上面动手,感觉应该很容易. 但是仔细看数据范围,发现如果不利用计数原理,肯定会超时,考虑数码出现的特征: \(A000\)到 ...

  7. linux c编程:文件的操作

    在Linux系统中,系统是通过inode来获得这个文件的信息.在Linux系统中,inode的信息都是封装在stat这个结构体中.可以通过man 2 stat来查看stat的具体结构.从中可以看到包含 ...

  8. 【docker】开启remote api访问,并使用TLS加密

    背景: docker默认是能使用本地的socket进行管理,这个在集群中使用的时候很不方便,因为很多功能还是需要链接docker服务进行操作,docker默认也可以开启tcp访问,但是这就相当于把整个 ...

  9. SQL Server分区表,能否按照多个列作为分区函数的分区依据(转载)

    问: Hi, I have a table workcachedetail with 40 million rows which has 8 columns.We decided to partiti ...

  10. linux中查找用户账户信息和登录信息的11中方法

    摘自:开源中国 微信公众号 1. id 2. groups 3. finger 4.getent 5. grep 6. lslogins 7..users 8. who 9. w 10. last或者 ...