POJ #3259 Wormholes 判负环

Description



  问题描述:链接

思路



  裸题,判断图是否有负环,用 bellman_ford 或者 spfa 。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int N, M, W; //顶点数 正环数 负权边数
const int MAX_N = 505;

struct Edge {
    int to;
    int T;
    Edge(int tt, int TT) : to(tt), T(TT) {}
};
vector<Edge> G[MAX_N];

void addEdge (const int& u, const int& v, const int& T) {
    G[u].push_back(Edge(v, T));
}

int dis[MAX_N];
bool inQueue[MAX_N];
int cnt[MAX_N];
bool spfa (const int& s) {
    memset(dis, INF, sizeof(dis));
    memset(inQueue, false, sizeof(inQueue));
    memset(cnt, 0, sizeof(cnt));
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    inQueue[s] = true;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        inQueue[u] = false;
        for (int j = 0; j < G[u].size(); ++j) {
            int v = G[u][j].to;
            int cost = G[u][j].T;
            if (dis[v] > dis[u] + cost ) {
                dis[v] = dis[u] + cost;
                if (!inQueue[v]) {
                    q.push(v);
                    inQueue[v] = true;
                    if (++cnt[v] > N) return false;
                }
            }
        }
    }
    return true;
}

int main(void) {
    int F;
    cin >> F;
    while (F--) {
        cin >> N >> M >> W;
        for (int i = 1; i <= N; i++) G[i].clear();
        for (int i = 1; i <= M; i++) {
            int S, E, T;
            cin >> S >> E >> T;
            addEdge (S, E, T);
            addEdge (E, S, T);
        }
        for (int i = 1; i <= W; i++) {
            int S, E, T;
            cin >> S >> E >> T;
            addEdge (S, E, -1 * T);
        }
        if (!spfa(1)) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bw98blogs/p/8450068.html

时间: 2024-10-07 12:53:41

POJ #3259 Wormholes 判负环的相关文章

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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 pa

Wormholes POJ - 3259 spfa判断负环

//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N=1e5,INF=0x3f3f3f3f; int dist[N]; int h[N],e[N],ne[N],w[N],idx; int n,m,z; void add(int a,int b,in

poj 3259 Wormholes (负权最短路,SPAF)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36641   Accepted: 13405 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

poj 3259 Wormholes 判断负权值回路

Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java class name: Main [Submit] [Status] [Discuss] Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A w

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 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[ 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 (bellman_ford算法判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32393   Accepted: 11771 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

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <