正常spfa中加入time数组,循环判断一个点是否入队并更新了n次以上注意是 > n!!其余的没有什么问题

扩展的还有,寻找所有负环上的点,这个可以在spfa中time 发现负环的时候,对那个点进行dfs操作,找到所有的负环上的点即可

void dfs(int u)
{
cir[u] = 1;
for(int i = id[u];~i;i = edge[i].pre)
{
if(!cir[edge[i].to])
dfs(edge[i].to);
}
}

一下负权回路代码以poj3259为例(poj又挂了,暂时还没发交题测试,欢迎大家来找bug)

/*
spfa负环的判断
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#define inf (1 << 29)
using namespace std;
typedef long long ll;
const int maxn = 600;
const int maxm = 3e3;
int id[maxn],cnt;
struct node{
int to,cost,pre;
}e[maxm * 2];
int dis[maxn];
int vis[maxn];
int time[maxn]; queue<int> q;
void init()
{
while(q.size())q.pop();
memset(time,0,sizeof(time));
memset(vis,0,sizeof(vis));
memset(id,-1,sizeof(id));
cnt = 0;
}
void add(int from,int to,int cost)
{
e[cnt].to = to;
e[cnt].cost = cost;
e[cnt].pre = id[from];
id[from] = cnt++;
}
int spfa(int s,int n)
{
for(int i= 0;i <= n;i++)
dis[i] = inf;
q.push(s);
vis[s] = 1;
time[s] = 1;
dis[s] = 0; while(q.size())
{
int now = q.front();
q.pop();
vis[now] = 0;
for(int i = id[now];~i;i = e[i].pre)
{
int to = e[i].to;
int cost = e[i].cost;
if(dis[to] > dis[now] + cost)
{
dis[to] = dis[now] + cost;
if(!vis[to])
{
vis[to] = 1;
q.push(to);
if(++time[to] > n)
return 0;
}
}
}
}
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
init();
int a,b,x;
for(int i = 1;i <= m;i++)
{
scanf("%d%d%d",&a,&b,&x);
add(a,b,x);
add(b,a,x);
}
for(int i = 1;i <= k;i++)
{
scanf("%d%d%d",&a,&b,&x);
add(a,b,-x);
}
int op = spfa(1,n); if(op)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}

spfa负环判断的更多相关文章

  1. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  2. 培训补坑(day5:最小生成树+负环判断+差分约束)

    补坑补坑((╯‵□′)╯︵┻━┻) 内容真的多... 一个一个来吧. 首先是最小生成树. 先讲一下生成树的定义 生成树就是在一张图上选取一些边,使得整个图上所有的点都连通. 那么我们要求的最小生成树有 ...

  3. POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49962   Accepted: 18421 Descr ...

  4. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  5. poj 2679 Adventurous Driving(SPFA 负环)

    /* - - 这题做了一天.....粗心害死人啊 题目描述恶心 数据更恶心... 先处理一下能走的边 能走的点(到这建边从终点跑一下.) 然后就是SPFA了 注意负环的判断 */ #include&l ...

  6. (简单) LightOJ 1074 Extended Traffic,SPFA+负环。

    Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...

  7. 题解—— 洛谷 p1993 小K的农场(差分约束&负环判断)

    看到题就可以想到差分约束 判断负环要用dfs,bfs-spfa会TLE 4个点 bfs-spfa #include <cstdio> #include <algorithm> ...

  8. LightOJ - 1074 Extended Traffic (SPFA+负环)

    题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...

  9. poj-3259 Wormholes(无向、负权、最短路之负环判断)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

随机推荐

  1. How do I configure a Wired Ethernet interface

    1.In order to configure the Wired Ethernet interface the MDI must be connected to the PC using the U ...

  2. 文档根元素 "mapper" 必须匹配 DOCTYPE 根 "configuration"

    该问题是因为xml的头部写错了,一个是configuration,一个是mapper,不能直接复制. 参考链接:http://blog.csdn.net/testcs_dn/article/detai ...

  3. 虚拟机安装centos7, 再安装gitlab 简单步骤

    先安装Linux centos7(朋友贡献的. Linux官网有下) 我自己用vm安装的. 未出现特殊状况 gitlab的搭建 安装基础包 yum -y install curl policycore ...

  4. msgs no .h file

    1.单独编译包,catkin_make --pkg 包名,failed,则 2.进入build下对应的msgs包中,使用make,以及make install,failed,则 3.使用catkin_ ...

  5. 2017/2/6:在oracle中varchar与varchar2的区别与增删改查

    1.varchar2把所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节:2.VARCHAR2把空串等同于null处理,而varchar仍按 ...

  6. javascript声明对象时 带var和不带var的区别

    2015/11/25补充: 关于变量声明这里有详细的解释: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Stat ...

  7. 使用xtrabackup备份innodb引擎的数据库

    innodb引擎的数据库可以使用mysqldump备份,如果表很大几十个G甚至上百G,显示用mysqldump备份会非常慢.然后使用xtrabackup 可以很快的在线备份innodb数据库.Inno ...

  8. Linux下进行程序设计时,关于库的使用:

    一.gcc/g++命令中关于库的参数: -shared: 该选项指定生成动态连接库: -fPIC:表示编译为位置独立(地址无关)的代码,不用此选项的话,编译后的代码是位置相关的,所以动态载入时,是通过 ...

  9. MySQL LOCK TABLES 与UNLOCK TABLES

    http://blog.csdn.net/zyz511919766/article/details/16342003 1语法 LOCK TABLES tbl_name[[AS] alias] lock ...

  10. 2018.10.25 bzoj4565: [Haoi2016]字符合并(区间dp+状压)

    传送门 当看到那个k≤8k\le 8k≤8的时候就知道需要状压了. 状态定义:f[i][j][k]f[i][j][k]f[i][j][k]表示区间[i,j][i,j][i,j]处理完之后的状态为kkk ...