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. C# 杀掉后台进程

    var p = Process.GetProcessesByName("WINWORD"); if (p.Any()) { for (int i = 0; i < p.Len ...

  2. hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩

    Description Recognizing junk mails is a tough task. The method used here consists of two steps:  1) ...

  3. phpstudy nginx下curl请求本地其他项目

    curl 请求的时候 如果用post请求,传递参数为 数组的时候 header 头 会被设置为  multipart/form-data  如果是字符串 形式 header 头会被设置为applica ...

  4. Fragment 生命周期:

    Fragment每个生命周期方法的意义.作用(注意红色的不是生命周期方法):setUserVisibleHint():设置Fragment可见或者不可见时会调用此方法.在该方法里面可以通过调用getU ...

  5. 【BZOJ4212】神牛的养成计划 Trie树+可持久化Trie树

    [BZOJ4212]神牛的养成计划 Description Hzwer成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变 ...

  6. 基于Flume的美团日志收集系统 架构和设计 改进和优化

    3种解决办法 https://tech.meituan.com/mt-log-system-arch.html 基于Flume的美团日志收集系统(一)架构和设计 - https://tech.meit ...

  7. java 线程 被相互排斥堵塞、检查中断演示样例解说----thinking java4

    package org.rui.thread.block; /** * 被相互排斥堵塞 就像在interrupting.java中看到的,假设你偿试着在一个对象上调用其synchronized方法, ...

  8. Java for LeetCode 113 Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. Ruby JSON操作

      解析来我们就可以使用以下命令来安装Ruby JSON 模块: ? 1 $gem install json 使用 Ruby 解析 JSON 以下为JSON数据,将该数据存储在 input.json ...

  10. mybatis中xml字段空判断及模糊查询

    由于业务特殊的查询需求,需要下面的这种查询,一直感觉模糊不清,本地测试一下顺便做个总结 贴一段xml代码,如下: <if test="receivedName != null and ...