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 的边,这样去检验有无环存 ...
随机推荐
- sealed,new,virtual,abstract与override关键字的区别?
1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...
- 美景听听Ai语音导游,助力华为荣耀PLAY手机发布
6月6日,荣耀PLAY科技酷玩新品发布会在北京大学生体育馆如期举办,美景听听Ai语音讲解助力新EUMI系统智慧旅行成新卖点,震撼登场! 随着生活水平的不断提升,出门旅行已经成了许多亲们释放压力.调节自 ...
- 【selenium+python】自动化测试目录与文件结构
一.首先,看一下完整的项目目录结构图,如下: ==================目录结构================== ==================目录结构============== ...
- 迁移,移动.vagrant.d目录
默认在 C:\Users\***\.vagrant.d 然后下面有boxes目录 想迁移到其它目录 setx VAGRANT_HOME "/d/.vagrant.d/" setx ...
- 3_Jsp标签_简单标签_防盗链和转义标签的实现
一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...
- django部署到apache上(非常重要的,3者版本要一致,是32位就都要是32位的)
网上把django部署到apache的文章挺多的,但是按照大家的操作,并没有一次就成功,还是遇到了一些问题,这里主要有以下几个情况. 1.网上找到的mod_wsgi的版本问题,导致动态库加载不上. 2 ...
- Java 集成域登陆
参考: 1. JAVA中使用jcifs集成AD域用户认证 2. https://liaosy.iteye.com/blog/1887092 3.其他库 https://github.com/Waffl ...
- MongoDB API java的使用
1.创建一个MongoDB数据库连接对象,它默认连接到当前机器的localhost地址,端口是27017. Mongo mongo=new Mongo(); 2.获得与某个数据库(例如“test”)的 ...
- js为Object对象动态添加属性和值 eval c.k c[k]
const appendInfo = () => { const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/maste ...
- 20179209《Linux内核原理与分析》第一周作业
如何揭开Linux操作系统的最大面纱 个人认为,真正理解一个操作系统最根本的就是理解其文件系统结构. 自windows图形界面诞生,国内大多数用户都选择了windows操作系统,很多人觉得window ...