UVA 558 Wormholes

利用SPFA判负环。如果一个节点出队N次有负环这里记录一下

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
int cnt[1010];
bool inq[1010];
int d[1010];
int w[1010][1010],N,M;
vector<int>G[1010];
queue<int>q;
bool SPFA()
{
    memset(cnt,0,sizeof(cnt));
    memset(inq,false,sizeof(inq));
    memset(d,0x3f,sizeof(d));
    while (!q.empty()) q.pop();
    q.push(0); cnt[0]++;
    inq[0] = true; d[0] = 0;
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        inq[u] = false;
        for (int i = 0; i < (int)G[u].size(); i++)
        {
            int v = G[u][i];
            if (d[v] > d[u] + w[u][v])
            {
                d[v] = d[u] + w[u][v];
                if (!inq[v])
                {
                    inq[v] = true;
                    cnt[v]++;
                    if (cnt[v] >= N) return true;
                    q.push(v);
                }
            }
        }
    }
    return false;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&N,&M);
        for (int i = 0; i < 1010; i++) G[i].clear();
        while (M--)
        {
            int u,v,we;
            scanf("%d%d%d",&u,&v,&we);
            G[u].push_back(v);
            w[u][v] =  we;
        }
        if (SPFA()) puts("possible");
        else puts("not possible");
    }
    return 0;
}
时间: 2024-10-14 00:59:20

UVA 558 Wormholes的相关文章

uva 558 Wormholes (Bellman-Ford算法判断负环)

uva 558 Wormholes In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties: Wormholes are one-way only. The time it takes to travel throu

UVA 558 Wormholes 【SPFA 判负环】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499 题意:就是判断图中有无负环 SPFA,某个节点入队次数大于n就是有负环. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algori

uva 558 tree(不忍吐槽的题目名)——yhx

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path. Input The input

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B