WA on#9 求助
查看原帖
WA on#9 求助
515129
TLEWA楼主2022/10/15 19:42

rt,代码:

#include<bits/stdc++.h>
#define maxnm 6050
#define int long long

using namespace std;

int T,n,m,u,v,w;

struct Node{
    int to,next,val;
}arr[maxnm];

int first[maxnm],p;

void add(int u,int v,int w) {
    arr[++p].next=first[u];
    first[u]=p;
    arr[p].to=v;
    arr[p].val=w;
}

int dis[maxnm],now,cnt[maxnm];
bool vis[maxnm];
queue<int> q;

bool SPFA(int from) {
    memset(dis,0x3f,sizeof(dis));
    dis[from]=0,vis[from]=1;
    q.push(from);

    while(!q.empty()) {
        now = q.front();
        q.pop();
        vis[now]=0;
        for(int i=first[now];i;i=arr[i].next) {
            if(dis[arr[i].to] > dis[now] + arr[i].val) {
                dis[arr[i].to] = dis[now] + arr[i].val;
                if(!vis[arr[i].to]) {
                    if(cnt[arr[i].to]>=n) return false;
                    ++cnt[arr[i].to];
                    q.push(arr[i].to),vis[v]=1;
                }
            }
        }
    }
    return true;
}

void init() {
    p=0;
    memset(arr,0,sizeof(arr));
    memset(first,0,sizeof(first));
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
}

signed main() {

    cin >> T;

    while(T--) {
        init();
        cin >> n >> m;
        for(int i=0;i!=m;++i) {
            cin >> u >> v >> w;
            add(u,v,w);
            if(w>=0) add(v,u,w);
        }
        if(SPFA(1)) cout << "NO" << endl;
        else cout << "YES" << endl;
    }

    return 0;
}
2022/10/15 19:42
加载中...