POJ 3259 Bellman_Ford算法

额。关键是读题。反正我是看了解题报告才知道意思的。给你n个点。m条路。双向的。耗费时间。w个虫洞。单向的。时间为负值。问你是否可以从某一点返回看到之前的自己。即为判断是不是有负环。用Bellman_Ford算法。

分分钟打完。排了好久的bug。还是循环那里j和i傻傻的分不清楚。

附代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define maxn 0x1f1f1f1f
using namespace std;

int n, m, w;  //定点数 m+w是边数

struct Edge
{
    int u, v, t;
} edge[6000];
int low[600];
int tot;

bool Bellman_Ford()
{
    memset(low, maxn, sizeof(maxn));
    //for (int i=2; i<=n; ++i)
       // low[i] = maxn;
    low[1] = 0;
    for (int i=1; i<n; i++)
    {
        bool flag = false;
        for (int j=0; j<tot; ++j)
        {
            if (low[edge[j].v] > low[edge[j].u] + edge[j].t)
            {
                low[edge[j].v] = low[edge[j].u] + edge[j].t;
                flag = true;
            }
        }
        if (!flag) break;
    }
    for (int i=0; i<tot; ++i)
    {
        if (low[edge[i].v] > low[edge[i].u] + edge[i].t)
            return true;  // 存在负环
    }
    return false;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d%d", &n, &m, &w);
        tot = 0;
        int u, v, t;
        for (int i=0; i<m; ++i)
        {
            scanf("%d%d%d", &u, &v, &t);
            edge[tot].u = u;
            edge[tot].v = v;
            edge[tot++].t = t;
            edge[tot].u = v;
            edge[tot].v = u;
            edge[tot++].t = t;
        }
        for (int i=0; i<w; ++i)
        {
            scanf("%d%d%d", &u, &v, &t);
            edge[tot].u = u;
            edge[tot].v = v;
            edge[tot++].t = -t;
        }
        bool flag = Bellman_Ford();
        if (flag) printf("YES\n");
        else printf("NO\n");
    }
return 0;
}

时间: 2024-10-29 19:05:30

POJ 3259 Bellman_Ford算法的相关文章

Wormholes - poj 3259 (Bellman-Ford算法)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34934   Accepted: 12752 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 d

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算法题解

本题其实也可以使用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

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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(最短路,判断有没有负环回路)

F - Wormholes Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatus Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a on

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

[2016-04-03][POJ][3259][Wormholes]

时间:2016-04-03 20:22:04 星期日 题目编号:[2016-04-03][POJ][3259][Wormholes] 题目大意:某农场有n个节点,m条边(双向)和w个虫洞(单向),(走虫洞可以回到过去的时间),问能否和过去的自己相遇 分析: 题目意思就是给定一个带负权的图,问是否存在负圈, spfa_bfs spfa_dfs bellman_ford #include <vector> #include <queue> #include <algorithm&

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 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28499   Accepted: 10302 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