100pts求调,码风优良
查看原帖
100pts求调,码风优良
1050375
czhusi楼主2025/7/20 09:12
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define int ll

const int N = 2e5 + 10, INF = LLONG_MAX / 2;

struct Node {
    int x, y, w;
};

vector<Node> v;

int bellman(int n) {
    vector<int> dis(n + 2, INF);
    dis[n + 1] = 0;
    int f = 0;
    for (int i = 1; i <= n; ++i) {
        f = 0;
        for (auto j : v) {
            if (dis[j.x] != INF && dis[j.y] > dis[j.x] + j.w) {
                dis[j.y] = dis[j.x] + j.w;
                f = 1;
            }
        }
    }
    
    for (auto j : v) {
        if (dis[j.x] != INF && dis[j.y] > dis[j.x] + j.w) {
            return 1; 
        }
    }
    return f; 
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
        int n, m;
        cin >> n >> m;
        v.clear();
        for(int i = 1, x, y, w; i <= m; i++) {
            cin >> x >> y >> w;
            v.push_back({x - 1, y, w});
            v.push_back({y, x - 1, -w});
        }
        for(int i = 0; i <= n; i++) {
            v.push_back({n + 1, i, 0});
        }
        
        if(!bellman(n)) {
            cout << "true\n";
        } else {
            cout << "false\n";
        }
    }
    return 0;
}
2025/7/20 09:12
加载中...