关于spfa被卡该怎么办
查看原帖
关于spfa被卡该怎么办
576817
Lyrella楼主2024/1/28 13:25
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll rd(){
    ll x = 0, f = 1; char c = getchar();
   while(! isdigit(c)){if(c == '-')f = - f; c = getchar();}
    while(isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
    return x * f;
}

const int N = 1e6 + 5;
int n, a[N], hd[N], cnt, d[N];
struct edge{
    int nxt, to, w;
}e[N];
void add(int u, int v, int w){
    e[++cnt] = {hd[u], v, w}; hd[u] = cnt;
}
bool vis[N];
int s, t;
void spfa(){
    memset(d, - 1, sizeof d);
    queue < int > q;
    d[s] = 0; vis[s] = true; q.push(s);
    while(! q.empty()){
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i = hd[u]; i; i = e[i].nxt){
            int v = e[i].to, w = e[i].w;
            if(d[v] < d[u] + w){
                d[v] = d[u] + w;
                if(! vis[v])q.push(v), vis[v] = 1;
            }
        }
    }
}

signed main(){
    int m = rd(); n = rd(); s = 0, t = m;
    for(int i = 1; i <= n; ++i){
        int l = rd(), r = rd(), c = rd();
        add(l - 1, r, c);
    }
    for(int i = s; i < t; ++i)add(i, i + 1, 0), add(i + 1, i, - 1);
    spfa();
    for(int i = 1; i <= m; ++i)printf("%d ", d[i] != d[i - 1]);
    return 0;
}

#19 2.21s

2024/1/28 13:25
加载中...