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. python 基础 9.3 mysql 数据操作

    #/usr/bin/python #coding=utf-8 #@Time   :2017/11/21 0:20 #@Auther :liuzhenchuan #@File   :mysql 数据操作 ...

  2. 从symbol link和hard link 到 unlink函数的一点记录

    之前一直对Linux的文件类型中的 “l” 类型的了解不是很深入,最近经过“圣经”指点,略知一二,在此先记录一下,以便以后查阅,之后会对文件和目录.文件I/O这部分再扩充. 首先需明确,Unix在查阅 ...

  3. 【BZOJ4710】[Jsoi2011]分特产 组合数+容斥

    [BZOJ4710][Jsoi2011]分特产 Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同 ...

  4. 升级webapi依赖的Newtonsoft.json的版本(转)

    随着微软日渐重视开源社区的贡献,微软在自己的产品中往往也会集成开源的第三方库. 比如System.Net.Http.Foramatting.dll 就依赖于Newtonsoft.json v4.5. ...

  5. java基本类型和包装类的区别(转)

    int 是基本类型,直接存数值 Integer是类,产生对象时用一个引用指向这个对象 Java把内存划分成两种:一种是栈内存,另一种是堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数 ...

  6. 【题解】CJOI2019 登峰造鸡境 (Prufer序列+斯特林数)

    [题解]CJOI2019 登峰造鸡境 (Prufer序列+斯特林数) 题目背景 舒服了. 题目描述 你有一颗n个点的无根树,每个点有有一个标号(1~n). 现在你知道,总共有m个叶子节点,求不同的树的 ...

  7. 我的Java开发学习之旅------>在Dos环境下Java内部类的编译和运行

    习惯了在IDE工具上进行代码编写,连最基本的Javac命令和Java命令都忘记的差不多了,今天对一个Java内部类进行编译和运行的时候,就出糗了.IDE是把双刃剑,它可以什么都帮你做了,你只要敲几行代 ...

  8. centos7 安装php

    http://blog.csdn.net/zhaozuosui/article/details/48394409

  9. imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。

    php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...

  10. 《Python Machine Learning》索引

    目录部分: 第一章:赋予计算机从数据中学习的能力 第二章:训练简单的机器学习算法——分类 第三章:使用sklearn训练机器学习分类器 第四章:建立好的训练集——数据预处理 第五章:通过降维压缩数据 ...