poj-3259-wormholes-spfa-判负环

题意:N个顶点, M条双向边, W条权值为负的单向边。求是否存在负环。

思路:首先你要懂bellman-ford或spfa。。这是基础的spfa判断是否存在负环的题,存在负环的节点会重复入队(因为最短路在不断变小), 所以只要有节点重复入队超过n次,即可判断存在负环(即开一个数组记录节点入队次数)。

总结:本来是想求出每对节点之间的最短路,看是否存在负的,结果果断TLE。后来才想起spfa可以处理负环云云~~

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<climits>
 7 #include<cstring>
 8 using namespace std;
 9 #define maxn 600
10 #define INF 10000000
11 struct node
12 {
13    int to, dist;
14 };
15 vector<node> g[maxn];
16 int n, m, w;
17 int cnt[maxn], d[maxn];
18 void input()
19 {
20    scanf("%d%d%d", &n, &m, &w);
21    for(int i = 0; i < n+10; i++) g[i].clear();
22    for(int i = 0; i < m; i++){
23       int a, b, c;
24       scanf("%d%d%d", &a, &b, &c);
25       g[a].push_back((node){b, c});
26       g[b].push_back((node){a, c});
27    }
28    for(int i = 0; i < w; i++) {
29       int a, b, c;
30       scanf("%d%d%d", &a, &b, &c);
31       c = -c;
32       g[a].push_back((node) {b, c});
33    }
34 }
35 bool spfa(int s)
36 {
37    for(int i = 0; i <= n; i++) d[i] = INF, cnt[i] = 0;
38    d[s] = 0;
39    cnt[s] = 1;
40    queue<int> q;
41    q.push(s);
42    bool inq[maxn]; memset(inq, 0, sizeof(inq));
43    inq[s] = true;
44    while(!q.empty()){
45       int t = q.front(); q.pop();
46       inq[t] = false;
47       int l = g[t].size();
48       for(int i = 0; i < l; i++){
49          int to = g[t][i].to, dist = g[t][i].dist;
50          if(d[to] > d[t] + dist) {
51             d[to] = d[t] + dist;
52             if(!inq[to]){
53                inq[to] = 1;
54                cnt[to]++;
55                if(cnt[to] >= n) return true;
56                q.push(to);
57             }
58          }
59       }
60    }
61    return false;
62 }
63 void work()
64 {
65    input();
66    if(spfa(1)) printf("YES\n");
67    else printf("NO\n");
68 }
69
70 int main()
71 {
72    int t ; cin>>t;
73    while(t--){
74       work();
75    }
76    return 0;
77 }

poj-3259-wormholes-spfa-判负环,布布扣,bubuko.com

时间: 2024-12-24 07:31:44

poj-3259-wormholes-spfa-判负环的相关文章

POJ 3259 Wormholes(SPFA判负环)

题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using na

poj 3259 Wormholes[ bellman_ford 判负环]

Wormholes Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wor

POJ 3259 Wormholes (判负环)

Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path

POJ 3259 Wormholes( bellmanFord判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

poj3259 Wormholes --- spfa判负环

又写了个bellman模板一直RE求解啊... #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x

「POJ3259」Wormholes - SPFA判负环

->戳我进原题 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 65211 Accepted: 24284 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-w

poj 3259 Wormholes spfa : 双端队列优化 判负环 O(k*E)

1 /** 2 problem: http://poj.org/problem?id=3259 3 spfa判负环: 4 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) 5 spfa双端队列优化: 6 维护队列使其dist小的点优先处理 7 **/ 8 #include<stdio.h> 9 #include<deque> 10 #include<algorithm> 11 using namespace std; 12 13 class Graph

POJ 3259 Wormholes SPFA算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

Poj3259--Wormholes(Spfa 判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36836   Accepted: 13495 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

POJ3259 Wormholes(SPFA判断负环)

Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac