poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860
题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rate,现在问你有v钱,从s开始出发交换钱能不能赚钱。
分析:如何存在正环,能无限增加钱,肯定可以赚了,因此用spfa判一下即可
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-9
#define N 1010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int v,next;
double R,C;
edge(){}
edge(int v,double R,double C,int next):v(v),R(R),C(C),next(next){}
}e[N<<];
int head[N<<],tot;
int vis[N],cnt[N];
int n,m,S;
double V,dis[N];
void init()
{
FILL(vis,);FILL(head,-);
FILL(dis,);FILL(cnt,);tot=;
}
void addedge(int u,int v,double r,double c)
{
e[tot]=edge(v,r,c,head[u]);
head[u]=tot++;
}
int spfa()
{
queue<int>que;
vis[S]=;cnt[S]=;
dis[S]=V;que.push(S);
while(!que.empty())
{
int u=que.front();
que.pop();vis[u]=;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
double w=(dis[u]-e[i].C)*e[i].R;
if(dis[v]<w)
{
dis[v]=w;
if(!vis[v])
{
vis[v]=;
cnt[v]++;
if(cnt[v]>n)return ;
que.push(v);
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%d%lf",&n,&m,&S,&V)>)
{
init();
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())puts("YES");
else puts("NO");
}
}
。
poj1860(spfa判正环)的更多相关文章
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- loj 1221(spfa判正环)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957 思路:由于路线为一个环,将路径上的权值改为c-p*d,那么 ...
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- [模板]SPFA判负环
目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...
- poj3621 SPFA判断正环+二分答案
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
随机推荐
- Delphi - SEH研究
技术交流,DH讲解. 前几天一个朋友在弄游戏外挂想带NP调试,就像自己来捕获游戏的异常.好像就要用到SEH这方面的知识.一起研究了一下,这里看下研究 和 在网上找的资料吧.SEH就是Structure ...
- perl 继承小例子
<pre name="code" class="html"><pre name="code" class="ht ...
- 基于visual Studio2013解决C语言竞赛题之1064互质数差1验证
题目 解决代码及点评 /* 64. 任意两个互质的自然数, 经过若干次加减后,总可获得结果为1的数值. 所谓互质数(即互素的数),是指这两个数除 1外再没有其它公因数. 如14,9为 ...
- 隐藏AutoCompleteTextView下拉框的滚动条
最近做项目需要用到AutoCompleteTextView这个控件,而其下拉框的滚动条有点难看,于是想去掉.走了些弯路,终于弄通了. 首先先介绍一种不靠谱的方法:反射机制 为什么会有人想到用这个呢? ...
- 利用Android属性动画实现Banner的原理与实践
事实上在Android刚推出属性动画的时候.就想利用它来设计一个Banner控件,一直没什么时间尝试. 在当时看我们应用中的Banner,使用计时器来控制自己主动播放,设置一个非常大的数,利用余数原理 ...
- FreeLink开源呼叫中心设计思想
上一篇大概说了国内外优秀的呼叫中心系统: 国内外优秀呼叫中心系统简单介绍 借鉴上述呼叫中心系统,我们的设计新一代呼叫中心例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY ...
- Eclipse Workspace Unavailable
开启Eclipse 提示: Workspace Unavailable: Workspace in use or cannot be created, choose a different one. ...
- shell oracle
#!/bin/sh traffic= rm -rf test.txt data=`sqlplus -S anoscfg/anoscfg <<EOF spool test.txt set f ...
- [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
- 一起来开发Android的天气软件(三)——使用Volley实现网络通信
距离上一篇一起来开发Android天气软件二的时间又将近半个月了,之间一直由于有事而没有更新实在抱歉,近期会加快更新的步伐.争取在2015年到来前写完这系列的博文,上一章我们已经使用LitePal框架 ...