Currency Exchange---poj1860 ( spfa, 回路,最长路)
题目链接:http://poj.org/problem?id=1860
题解:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;
#define N 210
#define INF 0xfffffff
double dist[N], V;
int cnt, Head[N], num[N], vis[N];
int n, m, s; struct Edge
{
int v, next;
double r, c;
}e[N]; void Add(int u, int v, double r, double c)
{
e[cnt].v = v;
e[cnt].r = r;
e[cnt].c = c;
e[cnt].next = Head[u];
Head[u] = cnt++;
} bool spfa()///spfa模板;
{
memset(vis, , sizeof(vis));
memset(num, , sizeof(num));
queue<int>Q;
vis[s] = ;
dist[s] = V;
Q.push(s);
num[s]++;
while(Q.size())
{
int p=Q.front();
Q.pop();
vis[p] = ;
for(int i=Head[p]; i!=-; i=e[i].next)
{
int q = e[i].v;
if(dist[q] < (dist[p] - e[i].c) * e[i].r)///注意松弛的变化;
{
dist[q] = (dist[p] - e[i].c) * e[i].r;
if(!vis[q])
{
vis[q] = ;
Q.push(q);
num[q] ++;
if(num[q]>n)
return true;///存在正权回路;
}
}
} }
if(dist[s]>V)///最长路后,实现了增值;
return true;
return false;
} int main()
{
int a, b;
double rab, rba, cab, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &V)!=EOF)
{
cnt = ;
memset(Head, -, sizeof(Head));
memset(dist, , sizeof(dist));
for(int i=; i<=m; i++)
{
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
Add(a, b, rab, cab);
Add(b, a, rba, cba);
}
if( spfa() )
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Currency Exchange---poj1860 ( spfa, 回路,最长路)的更多相关文章
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others) Memor ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- (简单) POJ 1860 Currency Exchange,SPFA判圈。
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ 1860: Currency Exchange 【SPFA】
套汇问题,从源点做SPFA,如果有一个点入队次数大于v次(v表示点的个数)则图中存在负权回路,能够套汇,如果不存在负权回路,则判断下源点到自身的最长路是否大于自身,使用SPFA时松弛操作需要做调整 # ...
随机推荐
- mysql驱动(github上的)
https://github.com/Eonblast/Emysql https://github.com/denglf/erlang-db-driver https://github.com/diz ...
- Visual Studio各版本一览!
上图红线标识处为常用版本,最经典的是VC++ 6.0,专为早期C++开发设计.红框标识处是其内部版本,如VS2008,其内部版本为vc9.0,注意查找区分! 目前,最新版本的VS2017已经发布,很大 ...
- JavaScript实现禁用键盘和鼠标的点击事件
编写自己定义的JavaScript函数maskingKeyboard()和rightKey(); maskingKeyboard():禁用键盘 rightKey():禁用鼠标右键 <script ...
- Android中Invalidate与postInvalidate的区别<转>
http://www.cnblogs.com/it-tomorrow/archive/2012/11/08/2760146.html 示例:http://rayleung.iteye.com/blog ...
- 使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h"
使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h",解决方法如下: 1>.在xcode中左边选中项目的root节点,在中间编 ...
- EL表达式格式化日期时间
1.首先引入标签库 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> ...
- C++中的三种继承public,protected,private
( c++默认class是private继承且class内的成员默认都是private struct 默认位public 继承,struct内成员默认是public ) 三种访问权限 public: ...
- 标准web浏览器的组件
浏览器基本上包括如下几个组件 1.HTML.XML.CSS.JavsScript解析器 2.Layout 3.文字和图形渲染 4.图像解码 5.GPU交互 6.网络访问 7.硬件加速
- rpm方式安装 gitlab centos7
一.使用RPM安装 - 推荐 官方推荐的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/ Tips 1 : Centos 7使用el7 ...
- 内网安全监控和预警平台架构设想(OSSIM)
内网安全监控和预警平台架构设想 需求简介 内网安全监控和预警平台是内网安全建设的物质基础,是所有甲方安全建设的必备武器库,无论是应急响应和追踪溯源,还是预知告警.自我清查:做下来总的体会是几个问题永远 ...