UVA558 - Wormholes(BellmanFord判负环)

UVA558 - Wormholes

题目大意:

有一个教授希望利用虫洞回到过去(还是从这个虫洞出来就到达了过去),给你虫洞形成的有向图,问教授能否回到过去。

解题思路:

利用BellmanFord判负环,如果不存在负环的话,那么最多经过N - 1次迭代就可以得到最短路,因为形成最短路最多N - 1个节点(起点不算),但是如果存在了负环,那么就可以一直迭代,最短路会越来越小。可以利用这个性质来判断是否存在负环。

代码:

#include <cstdio>
#include <cstring>
#include <queue>

const int maxn = 1005;
const int maxm = 2005;

using namespace std;

struct edge {
    int u, v;
    int d;
}e[maxm];

const int INF = 0x7f7f7f7f;
int N, M;
int d[maxn];

bool BellmanFord() {
    for (int i = 0; i < N; i++)
        d[i] = i == 0 ? 0 : INF;

    d[0] = 0;
    for (int k = 0; k < N - 1; k++)
        for (int i = 0; i < M; i++) {
            if (d[e[i].u] != INF && d[e[i].v] > d[e[i].u] + e[i].d)
                d[e[i].v] = d[e[i].u] + e[i].d;
        }

    for (int i = 0; i < M; i++)
        if (d[e[i].u] != INF && d[e[i].v] > d[e[i].u] + e[i].d)
            return true;
    return false;
}

int main () {

    int T;
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d%d", &N, &M);
        for (int i = 0; i < M; i++)
            scanf ("%d%d%d", &e[i].u, &e[i].v, &e[i].d);
        if (BellmanFord())
            printf ("possible\n");
        else
            printf ("not possible\n");
    }
    return 0;
}
时间: 2024-10-27 11:20:09

UVA558 - Wormholes(BellmanFord判负环)的相关文章

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

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(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 (判负环)

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

「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 (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

UVA - 11090 Going in Cycle!! (Bellman-Ford算法判负环)

Description I I U P C 2 0 06 Problem G: Going in Cycle!! Input: standard input Output: standard output You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a weight, which equals to sum of its edges. There

训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - 基础DP - BellmanFord - 图论 - 训练指南 Going in Cycle!! UVA - 11090 题意 就最小的环的平均权值 题解 分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)&