[poj1860] Currency Exchange (bellman-ford算法)
题目链接:http://poj.org/problem?id=1860
题目大意:给你一些兑换方式,问你能否通过换钱来赚钱?
使用ford算法,当出现赚钱的时候就返回YES,如果不能赚钱,则返回NO
应该是可以停下来的,但是我不会分析复杂度,谁来教教我?
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ size()
#define ST begin()
#define ED end()
#define CLR clear()
#define ZERO(x) memset((x),0,sizeof(x))
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const double EPS = 1e-; struct EDGE{
int from,to;
double r,c;
EDGE(int _from = , int _to = , double _r = , double _c = ) :
from(_from),to(_to),r(_r),c(_c) {}
}; vector<EDGE> vec;
int N,M,S;
double V;
double d[]; bool floyd(int s){
d[s] = V;
bool flag = false;
while(true){
flag = false;
for(int i=;i<vec.SZ;i++){
const EDGE& e = vec[i];
if(d[e.from]>0.0 && d[e.to]<(d[e.from]-e.c)*e.r){
d[e.to]=(d[e.from]-e.c)*e.r;
flag = true;
}
}
if(d[s]>V) return true;
if(!flag) {
break;
}
}
return d[s]>V;
} int main(){
while(EOF!=scanf("%d%d%d%lf",&N,&M,&S,&V)){
vec.CLR;
ZERO(d);
int from,to;
double rab,cab,rba,cba;
for(int i=;i<M;i++){
scanf("%d%d%lf%lf%lf%lf",&from,&to,&rab,&cab,&rba,&cba);
vec.PB(EDGE(from,to,rab,cab));
vec.PB(EDGE(to,from,rba,cba));
}
puts(floyd(S)?"YES":"NO");
}
return ;
}
[poj1860] Currency Exchange (bellman-ford算法)的更多相关文章
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- POJ1860:Currency Exchange(BF)
http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- POJ1860 Currency Exchange【最短路-判断环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ-1860 Currency Exchange( Bellman_Ford, 正环 )
题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...
随机推荐
- 省常中模拟 day2
第一题: 题目大意: 有mn颗糖,要装进k个盒子里,使得既可以平均分给n个人,也可以平均分给m个人. 求k的最小值. 解题过程: 1.先看一组小数据(13,21).那么根据贪心的原则很容易想到先拿13 ...
- 【个人使用.Net类库】(3)Excel文件操作类(基于NPOI)
Web开发工作中经常要根据业务的需要生成对应的报表.经常采用的方法如下: 将DataTable导出至Excel文件; 读取模板Excel文件; 修改模板Excel文件对应的内容. 因此,便想到封装一个 ...
- powershell玩转sqlite数据库
脚本经常需要处理文本,有时候是行列整齐文本.那么powershell脚本处理行列文本有几种方法呢?一种是excel,另外的一些是?access?sqlite? sqlite是一个很小巧的,很方便嵌入到 ...
- WebView WebViewClient WebChromeClient
在android中,浏览器的功能分成几个部分,每个部分分工明确,互相协作.其中: 1. WebView :专门负责网页数据解析和渲染: 2. WebViewClient :帮助WebView处理各种请 ...
- 安装debian第一天遇到的几个问题及解决方案
1.当我想要使用sudo时,提示 bash: sudo: command not found 一开始以为是PATH不对,就各种百度各种试 export PATH=${PATH}:$HOME/bin:/ ...
- PATH变量
PATH变量由系统管理员配置,通常包含如下一些存储系统程序的标准路径: /bin 二进制文件目录,用于存放启动系统时用到的路径 /usr/bin 用户二进制文件目录,用于存放用户使用的标准程序 / ...
- Spring框架学习(二)
一.依赖注入的三种注入方式 Spring框架为我们提供了三种注入方式:set注入.构造方法注入和接口注入. 1.set注入 规律:无论给什么赋值,配置文件中<property>标签的nam ...
- Windows下利用py2exe生成静默运行的命令行程序
py2exe是python的第三方库,可以利用它将你的python脚本编译成可执行文件(exe),而在实际的开发过程中生成的dos窗口很影响用户体验,建议按以下方式让exe静默运行. 首先将你的pyt ...
- activity 和 生命周期 :流程
activity是android的一个基本的组件.讨论生命周期,taskstack等等的话题的时候.就不得不去看一下android framework层的源码了. 生命周期,实际就是系统调用andro ...
- xml json protobuf
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...