UVAlive6800The Mountain of Gold?(负环)

Posted zwfymqz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVAlive6800The Mountain of Gold?(负环)相关的知识,希望对你有一定的参考价值。

题意

题目链接

问从(0)出发能否回到(0)且边权为负

Sol

先用某B姓算法找到负环,再判一下负环上的点能否到(0)

#include<bits/stdc++.h>
#define chmax(x, y) (x = (x > y ? x : y))
#define chmin(x, y) (x = (x < y ? x : y))
#define Pair pair<int, int>
#define MP make_pair
#define fi first
#define se second 
using namespace std;
const int MAXN = 2e6 + 10, INF = 1e9 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, num, dis[MAXN], vis[MAXN], gg[MAXN];
struct Edge {
    int u, v, w;
}E[MAXN];
vector<int> v[MAXN];
void AddEdge(int x, int y, int z) {
    v[x].push_back(y);
    E[++num] = (Edge) {x, y, z};
}
bool dfs(int x) {
//  printf("%d
", x);
    gg[x] = 1;
    if(vis[x] != -1) return vis[x];
    if(x == 1) return vis[x] = 1;
    
    bool flag = 0;
    for(int i = 0; i < v[x].size(); i++) {
        int to = v[x][i];
        if(!gg[to] && dfs(to)) {flag = 1; break;}
    }
    return vis[x] = flag;
}
bool SPFA() {
    dis[1] = 0;
    for(int i = 1; i < N; i++)
        for(int j = 1; j <= M; j++)
            chmin(dis[E[j].v], dis[E[j].u] + E[j].w);
    for(int i = 1; i <= M; i++) {
        int x = E[i].u, y = E[i].v;
        if((dis[y] > dis[x] + E[i].w) && dfs(y)) return 1;
    }
    return 0;
}
void init() {
    for(int i = 1; i <= N; i++) v[i].clear();
    memset(vis, -1, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));
    memset(gg, 0, sizeof(gg));
    num = 0;
}
void solve(int Case) {
    N = read(); M = read();
    init();
    for(int i = 1; i <= M; i++) {
        int x = read() + 1, y = read() + 1, z = read();
        AddEdge(x, y, z);
    } 
    printf("Case #%d: ", Case); puts(SPFA() ? "possible" : "not possible");
}
int main() {
    //freopen("a.in", "r", stdin);
    for(int T = read(), i = 1; i <= T; solve(i++));
    return 0;
}

以上是关于UVAlive6800The Mountain of Gold?(负环)的主要内容,如果未能解决你的问题,请参考以下文章

ACM-ICPC 2017 Asia Urumqi G. The Mountain

UVa 11167 Monkeys in the Emei Mountain (最大流)

UVALive - 3942 Remember the Word[树状数组]

UVALive - 3942 Remember the Word[Trie DP]

Find the Border UVALive - 3218 (卷包裹)

Find the Border UVALive - 3218(PSGL)