POJ - 1860 Bellman-Ford判正环
心累,陕西邀请赛学校不支持,可能要自费了。。
思路:套用Bellman-Ford判断负环的思路,把大于改成小于即可判定是否存在从源点能到达的正环。如果存在正环,那么完全多跑几次正环就可以把钱增加到足够返回到S并且大于原来的金额。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 200 + 5;
int u[maxn], v[maxn];
double r[maxn], c[maxn];
double d[maxn];
bool bell(double st, int s, int n, int m) {
for(int i = 0; i < n; ++i) d[i] = -inf;
d[s] = st;
for(int i = 0; i < n-1; ++i)
for(int j = 0; j < m; ++j) {
int x = u[j], y = v[j];
if(d[x] > -inf) d[y] = max(d[y], (d[x] - c[j])*r[j]);
}
for(int i = 0; i < m; ++i) {
int x = u[i], y = v[i];
if(d[y] < (d[x] - c[i])*r[i] && d[x] > -inf) return true; //存在正环
}
return false;
}
int main() {
int n, m, s;
double V;
while(scanf("%d%d%d%lf", &n, &m, &s, &V) == 4) {
int cur = 0;
for(int i = 0; i < m; ++i) {
scanf("%d%d", &u[cur], &v[cur]);
scanf("%lf%lf", &r[cur], &c[cur]);
++cur;
u[cur] = v[cur-1], v[cur] = u[cur-1];
scanf("%lf%lf", &r[cur], &c[cur]);
++cur;
}
if(bell(V, s, n, cur)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
如有不当之处欢迎指出!
POJ - 1860 Bellman-Ford判正环的更多相关文章
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- POJ 2240 Arbitrage(判正环)
http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- poj 1860 bellman 求正环
#include<stdio.h> #include<string.h> #include<queue>//只需判断是否有正环路径就可以了 using namesp ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
随机推荐
- js_7_dom文本
dom编程核心? 绑定事件 找到标签(innerText找标签里文本内容,innerHTML找标签里所有内容) 特殊的标签:input,select,textarea ,通过.value来获取值 3. ...
- Python---socketserver
为方便以后学习和查询,特从socketserver架构.源码分析和案例三方面进行总结 1. Python之socketserver架构 2. Python之socketserver源码分析(一) 3. ...
- js实现最短时间走完不同速度的路程
题目: 现在有一条公路,起点是0公里,终点是100公里.这条公路被划分为N段,每一段有不同的限速.现在他们从A公里处开始,到B公里处结束.请帮他们计算在不超过限速的情况下,最少需要多少时间完成这段路程 ...
- 安卓和IOS兼容问题
点击穿透 click延迟 scroll元素临界的bug android screen.w/h 不准 rem不准 scroll时动画失效 animate回调 最小字号限制 不同机型全屏自适应 andro ...
- jenkins持续集成简介[一]
标签(linux): jenkins 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 什么时持续集成? Continuous integration(CI) 持续 ...
- JAVA设计模式之---工厂模式
1.引言 工厂模式可以分为类: 1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory) 这种模式从上 ...
- manacher 模板
求最长回文子序列的 O(n)做法 讲解 #include <iostream> #include <cstdio> #include <algorithm> #in ...
- BZOJ 2463: [中山市选2009]谁能赢呢?[智慧]
明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问过.谁不能移 ...
- 51Nod 欢乐手速场1 C 开心的小Q[莫比乌斯函数]
开心的小Q tangjz (命题人) quailty (测试) 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个数字存在一个约数是完全平方数,那么小Q就认为这个数是有趣的 ...
- MessagePack简析
一.MessagePack是什么 先看官方的定义:MessagePack是一种高效的二进制序列化格式.它允许您像JSON一样在多个语言之间交换数据.但是,它更快并且更小. 从官方定义中,可以有如下的结 ...