POJ3259负环判定
题意:有n个顶点,m条边,然后有w个洞,过每个洞的时间为-ti,求是否会时光倒流
分析:就是求是否存在负圈,用Bellman-Floyd判定是否存在负圈即可,注意是无向图,所以路径是双向可达的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
const int INF=<<;
struct edge{
int from,to,cost;
};
edge es[maxn];
int n,m,w;
int d[maxn]; bool find_negative_loop(){
memset(d,,sizeof(d));
for(int i=;i<n;i++){
for(int j=;j<m*+w;j++){
edge e=es[j];
if(d[e.to]>d[e.from]+e.cost){
d[e.to]=d[e.from]+e.cost; if(i==n-) return true;
}
//cout<<d[e.to]<<endl;
}
}
return false;
} int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d%d",&n,&m,&w);
for(int i=;i<m*;i+=)
{
int s,e,t;
scanf("%d%d%d",&s,&e,&t);
es[i].from=s,es[i].to=e,es[i].cost=t;
es[i+].from=e,es[i+].to=s,es[i+].cost=t;
}
for(int i=m*;i<w+m*;i++)
{
int s,e,t;
scanf("%d%d%d",&s,&e,&t);
es[i].from=s,es[i].to=e,es[i].cost=-t;
}
if(find_negative_loop()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
POJ3259负环判定的更多相关文章
- POJ-2175 Evacuation Plan 最小费用流、负环判定
题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- UVA 558 判定负环,spfa模板题
1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...
- POJ-3259 Wormholes---SPFA判断有无负环
题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- POJ3259 Wormholes 【spfa判负环】
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj3259(spfa判负环)
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...
- POJ3259(Wormholes) 判断负环
题意: 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞 ...
随机推荐
- swool安装
swoole安装 安装完PHP后,即可安装swoole扩展. swoole扩展下载地址:https://github.com/swoole/swoole-src/releases 尽量选择stable ...
- CentOS 下配置CUPS
一.共享打印机的设置 1.在http://www.openprinting.org/printer/HP/HP-LaserJet_1010检查打印机的支持情况,两个企鹅以上表示Mostly,支持大部分 ...
- java数据结构之链表的实现
这个链表的内部是使用双向链表来表示的,但是并未在主函数中进行使用 /** * 链表 * 2016/4/26 **/ class LinkList{ Node head = new Node(); No ...
- UVALive - 4026 Difficult Melody(暴力)
我这个英语学渣又把题给翻译错了……(话说,六级差十分没有过,好心疼T T),题目中说的P和Q都是计算game的个数,我以为是出现的次数,各种wa..后来调整了以后又是各种wa,原来是double型的数 ...
- FZU 1894 志愿者选拔(优化循环)
我并没有多想,他们是用的数组模拟队列,然而我就是优化循环次数过得 #include<iostream> #include<cstdio> #include<cstring ...
- CentOS下编译安装Apache2(新)
官网下载apache,apr, apr-util,pcre httpd-2.4.16.tar.gz http://httpd.apache.org/download.cgi#apache24 apr- ...
- Excel工作表 表名导出
Technorati 标签: microsoft office,vbs,excel 1: Attribute VB_Name = "表名导出" 2: Sub test() 3: ...
- ubuntu服务器移植步骤
1.安装LAMP套件 1 tasksel 2.安装FTP工具 http://www.cnblogs.com/esin/p/3483646.html 3.安装PHPMyAdmin 1)安装 1 apt- ...
- 干货分享:MySQL之化险为夷的【钻石】抢购风暴【转载】
转自: 干货分享:MySQL之化险为夷的[钻石]抢购风暴 - Vanos_韩尛哲 - 博客园http://www.cnblogs.com/Vanos-lcp/p/5642097.html 抢购钻石不稀 ...
- 【floyd】 poj 2240
#include <iostream> #include <map> #include <string> #include <memory.h> usi ...