题目链接:http://poj.org/problem?id=3259

题目大意是给你n个点,m条双向边,w条负权单向边。问你是否有负环(虫洞)。

这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1e3 + ;
const int INF = 1e9;
struct data {
int next , to , cost;
}edge[MAXN * ];
int head[MAXN] , d[MAXN] , cnt[MAXN] , cont;
bool vis[MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
d[i] = INF;
cnt[i] = ;
head[i] = -;
vis[i] = false;
}
cont = ;
} inline void add(int u , int v , int cost) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].cost = cost;
head[u] = cont++;
} bool spfa(int s , int n) {
d[s] = ;
queue <int> que;
while(!que.empty()) {
que.pop();
}
que.push(s);
while(!que.empty()) {
int temp = que.front();
que.pop();
vis[temp] = false;
for(int i = head[temp] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(d[v] > d[temp] + edge[i].cost) {
d[v] = d[temp] + edge[i].cost;
if(!vis[v]) {
que.push(v);
vis[v] = true;
cnt[v]++;
if(cnt[v] >= n)
return true;
}
}
}
}
return false;
} int main()
{
int n , m , c , u , v , cost , t;
scanf("%d" , &t);
while(t--) {
scanf("%d %d %d" , &n , &m , &c);
init(n);
while(m--) {
scanf("%d %d %d" , &u , &v , &cost);
add(u , v , cost);
add(v , u , cost);
}
while(c--) {
scanf("%d %d %d" , &u , &v , &cost);
add(u , v , -cost);
}
if(spfa( , n)) {
printf("YES\n");
}
else {
printf("NO\n");
}
}
}

POJ 3259 Wormholes(SPFA判负环)的更多相关文章

  1. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  2. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  3. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  4. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  5. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  6. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  7. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  8. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  9. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

随机推荐

  1. ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking

    #!/bin/bash # # ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking # 说明: # 本文主要对TI的sdk中 ...

  2. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  3. C++实现String

    # include <iostream> # include <memory> # include <cstring> using namespace std; c ...

  4. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  5. TS 流解码过程

    TS 流解码过程: 1. 获取TS中的PAT 2. 获取TS中的PMT 3. 根据PMT可以知道当前网络中传输的视频(音频)类型(H264),相应的PID,PCR的PID等信息. 4. 设置demux ...

  6. executeQuery,executeUpdate 和 execute 区别

    http://www.360doc.com/content/14/0315/09/16068204_360719186.shtml http://i-feng.iteye.com/blog/17066 ...

  7. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...

  8. ArcMap自定义脚本工具制作

    原文 ArcMap自定义脚本工具制作 在制图的前期,一般需要做一些数据的整理,如图层合并.裁剪等工作.虽然在ArcMap中也有提供对应的工具,但使用起来需要点技巧.如批量裁剪,虽然可以实现,但出来的结 ...

  9. codeforces 675E Trains and Statistic 线段树+贪心统计

    分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...

  10. QC开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改

    今天在QC9.0项目中增加了几个项目,然后我的想法是:开发只能修改指派给自己的缺陷,而其他的bug可以查看但是不允许修改 虽说qc我还是比较熟悉的,但是对于这个问题,感觉可能要用到脚本,对于脚本我一窍 ...