题目链接: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. bzoj3252

    简单题,每次取出最长链,然后对于练上每个点x,终点在其子树内的链都要减去a[x] 这显然可以用dfs序+线段树维护 显然每个点只要删一次即可,复杂度是O(nlogn) type node=record ...

  2. LA 3516 (计数 DP) Exploring Pyramids

    设d(i, j)为连续子序列[i, j]构成数的个数,因为遍历从根节点出发最终要回溯到根节点,所以边界情况是:d(i, i) = 1; 如果s[i] != s[j], d(i, j) = 0 假设第一 ...

  3. Self-Paced Training (3) - Docker Operations

    AgendaTroubleshooting ContainersOverview of Security PracticesPrivate RegistryIntro to Docker Machin ...

  4. android-async-http

    安装 http://blog.csdn.net/wangwei_cq/article/details/9453345 包内的一些基本的参数 http://www.cnblogs.com/manuose ...

  5. UVALive 4043 Ants(二分图完美匹配)

    题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...

  6. MySQL基础之第5章 操作数据库

    假设已经登录 mysql-h localhost -uroot -proot 5.1.显示.创建.删除数据库 show databases;     显示所有的数据库 create database ...

  7. Linux User's Manual IOSTAT

    IOSTAT(1) Linux User's Manual IOSTAT(1) NAME iostat - Report Central Processing Unit (CPU) statistic ...

  8. PagerSlidingTabStrip 高亮选中标题

    1.选中标题后,高亮标题@Override public void onPageSelected(int position) { setSelectTextColor(position); if (d ...

  9. Mahout分步式程序开发 聚类Kmeans(转)

    Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...

  10. 最短路+线段交 POJ 1556 好题

    // 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...