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 ...
随机推荐
- GreenDAO数据库版本升级
GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁. 初始化数据库模型代码的时候需要使用java项目生成代码,依赖的jar包已经上传到我的资源里了,下载地址如下:ht ...
- Windows Azure 安全最佳实践 - 第 4 部分:需要采取的其他措施
那么,哪些安全威胁应由WindowsAzure环境缓解?哪些安全威胁必须由开发人员缓解? 开发 Windows Azure 应用程序的最佳安全做法一文说明了对于在 Windows Azure 中运行的 ...
- Eclipse设置Android Logcat输出字体大小
Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...
- C++学习之路—继承与派生(四)拓展与总结
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1 拓展部分 本节主要由两部分内容组成,分 ...
- 开源力量公开课第三十期- 跟我一起玩转OpenStack
开源力量公开课第三十期- 跟我一起玩转OpenStack 开课时间:2013年9月10日 18:30 - 21:30 形式:现场(北京3W咖啡) + 线上直播, 免费报名:http://www.o ...
- kgdb调试注意事项
0 首先提下注意事项的背景: kgdb和printk共用一个串口 1 设置波特率: //最高支持460800波特率 arm-eabi-gdb ./vmlinux ...
- NumberFormat 类
NumberFormat 表示数字的格式化类, 即:能够依照本地的风格习惯进行数字的显示. 此类的定义例如以下: public abstract class NumberFormat extends ...
- 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp
模型实现代码,关键是train函数和predict函数,都很容易. #include <iostream> #include <string> #include <mat ...
- spring获取bean 实例
ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...
- must return an Iterable of arrays.(junit4)
java.lang.Exception: TestIterator.init() must return an Iterable of arrays. at org.junit.runners.Par ...