zoukankan      html  css  js  c++  java
  • 洛谷 P3385 【模板】负环

    传送门

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const ll _inf(0x3f3f3f3f3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(2e3 + 10);
    const int maxm(3e3 + 10);
    bool vis[maxn];
    int ecnt, head[maxn], cnt[maxn], dis[maxn];
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm << 1];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    bool spfa(int tot)
    {
        queue<int> q;
        vis[1] = true;
        dis[1] = 0;
        q.push(1);
        while (not q.empty()) {
            int u = q.front();
            q.pop();
            vis[u] = false;
            for (int i = head[u]; ~i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    if (not vis[v]) {
                        cnt[v] = cnt[u] + 1;
                        if (cnt[v] >= tot) {
                            return true;
                        }
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        return false;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int t = read();
        while (t--) {
            ecnt = 0;
            memset(head, -1, sizeof head);
            memset(dis, 0x3f, sizeof dis);
            memset(vis, false, sizeof vis);
            memset(cnt, 0, sizeof cnt);
            int n = read(), m = read();
            while (m--) {
                int u = read(), v = read(), w = read();
                addEdge(u, v, w);
                if (w >= 0) {
                    addEdge(v, u, w);
                }
            }
            printf("%s
    ", spfa(n) ? "YES" : "NO");
        }
        return 0;
    }
    
  • 相关阅读:
    常用的android弹出对话框
    Android 启动后台运行程序(Service)
    java 日期获取时间戳
    stringbuffer与stringbuilder的区别
    Linux记录-sysctl.conf优化方案
    Linux记录-salt分析
    Hadoop记录-fair公平调度队列管理
    Linux记录-GC分析
    Hadoop记录-hdfs转载
    Linux记录-salt-minion安装
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13839410.html
Copyright © 2011-2022 走看看