P3385 【模板】负环 (spfa模板,判断负环)

P3385 【模板】负环

题目链接

code

#include <iostream>
#include <cstring>
#include <string>
#include <stack>
#include <queue>

using namespace std;
const int N = 2e3 + 5, M = 3e5 + 5, INF = 0x3f3f3f3f;
int n, m;
struct edge {
    int v, w, next;
} e[M << 1];
int head[N], tot;
int dis[N];
int num[N];
bool inq[N];

void add_edge(int u, int v, int w) {
    e[tot] = {v, w, head[u]};
    head[u] = tot++;
}

bool spfa() {
    // 判断是否能形成负环
    for (int i = 1; i <= n; ++i) {
        dis[i] = INF, num[i] = 0, inq[i] = false;
    }
    dis[1] = 0, num[1] = 1, inq[1] = true;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for (int i = head[u]; i != -1; i = e[i].next) {
            int v = e[i].v;
            if (dis[v] > dis[u] + e[i].w) {
                dis[v] = dis[u] + e[i].w; // 更新dis[v]
                if (!inq[v]) {
                    num[v]++;
                    if (num[v] >= n) return true; // 注意这里写 >= 不然会TLE ???就很奇怪。同一个点进入更新队列大于等于n次说明这个点在一个负环上
                    inq[v] = true;
                    q.push(v);
                }
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        cin >> n >> m;
        memset(head, -1, sizeof(head));
        tot = 0;
        while (m--) {
            int u, v, w;
            cin >> u >> v >> w;
            add_edge(u, v, w);
            if (w >= 0) add_edge(v, u, w);
        }
        if (spfa()) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

code 2

#include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;
const int N = 2e3 + 5, INF = 0x3f3f3f3f;
struct edge {
    int v, w;
};
vector<edge> G[N];
int n, m;
int dis[N];
int num[N];
bool inq[N];

bool spfa() {
    // 判断是否能形成负环
    for (int i = 1; i <= n; ++i) {
        dis[i] = INF, num[i] = 0, inq[i] = false;
    }
    dis[1] = 0, num[1] = 1, inq[1] = true;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for (auto v : G[u]) {
            if (dis[v.v] > dis[u] + v.w) {
                dis[v.v] = dis[u] + v.w;
                if (!inq[v.v]) {
                    num[v.v]++;
                    if (num[v.v] >= n) return true;
                    inq[v.v] = true;
                    q.push(v.v);
                }
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        cin >> n >> m;
        for (int i = 0; i <= n; ++i) {
            G[i].clear();
        }
        while (m--) {
            int u, v, w;
            cin >> u >> v >> w;
            G[u].push_back({v, w});
            if (w >= 0) G[v].push_back({u, w});
        }
        if (spfa()) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值