POJ1860(ford判环)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 24243 | Accepted: 8813 |
Description
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
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
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判环)的更多相关文章
- POJ3259(ford判环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 39078 Accepted: 14369 Descr ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- hdu4888 Redraw Beautiful Drawings 最大流+判环
hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/6553 ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- Dwarves (有向图判环)
Dwarves 时间限制: 1 Sec 内存限制: 64 MB提交: 14 解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...
- COJ 3012 LZJ的问题 (有向图判环)
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- E - Andrew and Taxi-二分答案-topo判环
E - Andrew and Taxi 思路 :min max 明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...
随机推荐
- K均值算法总结
这几天在一个项目上需要用到K均值聚类算法,以前都是直接利用百度老师copy一个Kmeans算法代码,这次想自己利用已知的算法思想编写一下,编写才知道,虽然熟悉了算法思想,真正实现时,还是遇到不少bug ...
- 安装部署Solrcloud
实验说明: 三台虚拟机做solrcloud集群 安装solr前请确保jdk .tomcat.zookeeper已安装好,否则无法启动 三台虚拟机I ...
- Vim 打开文件同时定位到某一行
在linux下,当后台某一行报警出错后,想用vim打开文件同时定位到某一行, Vim +某一行 filename 即可.
- Java提高(二)---- HashTable
阅读博客 java提高篇(二五)—–HashTable 这篇博客由chenssy 发表与2014年4月,基于源码是jdk1.7 ========================== 本文针对jdk1. ...
- XP,32/64位Win7,32/64位Win8,32/64位Win10系统 【春节版】
本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...
- EasyDSS RTMP流媒体解决方案之Windows服务安装方案
Windows服务安装 EasyDSS_Solution流媒体解决方案,可以通过start一键启动.在实际应用中,我们希望可以设置成系统服务,那么下面我将会介绍,如何在windows中将流媒体解决方案 ...
- redis自启动
$ vi /etc/init.d/redis # chkconfig: 2345 90 10 # description: Redis is a persistent key-value databa ...
- NinjaFramework中文教程(简单版)-手把手教程-从零开始
第一步: 官网http://www.ninjaframework.org/documentation/getting_started/create_your_first_application.htm ...
- (转)source insight 窗口嵌入
昨天用了一下source insight ,都说很强大,也有感觉,但是这个强大的东西往往不是那么容易弄清楚的,或者一下子就好上手的,工具强大,功能复杂多样,一开始不知道怎么入手,以后慢慢来吧,学习是要 ...
- SpringBoot学习笔记(10):使用MongoDB来访问数据
SpringBoot学习笔记(10):使用MongoDB来访问数据 快速开始 本指南将引导您完成使用Spring Data MongoDB构建应用程序的过程,该应用程序将数据存储在MongoDB(基于 ...