poj 3259 Wormholes(bellman-ford判断负环)

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

题目就是问你能否回到原点而且时间还倒回去了。题目中有些路中有单向的虫洞能让时间回到过去

所以只要将虫洞这条边的权值赋为负然后再判断有没有负环就行了。

#include <iostream>
#include <cstring>
using namespace std;
const int inf = 10001;
int f , n , m , w ,dis[1001] , counts;
struct TnT {
    int u , v , weight;
}T[5200];
void relax(int u , int v , int weight) {
    if(dis[v] > dis[u] + weight)
        dis[v] = dis[u] + weight;
}
bool bellman_ford() {
    for(int i = 1 ; i < n ; i++) {
        for(int j = 1 ; j <= counts ; j++) {
            relax(T[j].u , T[j].v , T[j].weight);
        }
    }
    bool flag = true;
    for(int i = 1 ; i <= counts ; i++) {
        if(dis[T[i].v] > dis[T[i].u] + T[i].weight) {
            flag = false;
            break;
        }
    }
    return flag;
}
int main() {
    cin >> f;
    int s , e , t;
    while(f--) {
        cin >> n >> m >> w;
        counts = 0;
        memset(dis , inf , sizeof(dis));
        for(int i = 1 ; i <= m ; i++) {
            cin >> s >> e >> t;
            T[++counts].u = s , T[counts].v = e , T[counts].weight = t;
            T[++counts].u = e , T[counts].v = s , T[counts].weight = t;
        }
        for(int i = 1 ; i <= w ; i++) {
            cin >> s >> e >> t;
            T[++counts].u = s , T[counts].v = e , T[counts].weight = -t;
        }
        if(bellman_ford()) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
        }
    }
    return 0;
}
时间: 2024-08-06 19:46:42

poj 3259 Wormholes(bellman-ford判断负环)的相关文章

(简单) POJ 3259 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

poj 3259 Wormholes【spfa判断负环】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 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【Bellman_ford判断负环】

题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&

解题报告:poj 3259 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

POJ 3259 Wormholes Bellman题解

本题就是需要检查有没有负环存在于路径中,使用Bellman Ford算法可以检查是否有负环存在. 算法很简单,就是在Bellman Ford后面增加一个循环判断就可以了. 题目故事很奇怪,小心读题. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const int MAX_W = 201; struct E

POJ 3259 虫洞旅行 spfa判负环

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31425   Accepted: 11431 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判断负环】

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

POJ 3259 Wormholes【最短路/SPFA判断负环模板】

农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞.FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间.也许他就能遇到自己了:).为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5).所有的路径所花时间都