社区讨论

过样例了怎么只有10分

P3385【模板】负环参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lz1zqnsw
此快照首次捕获于
2024/07/26 08:55
2 年前
此快照最后确认于
2024/07/26 10:11
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=2001,M=1e6+1;
int he[N],ver[M],nxt[M],w[M],dis[N],st[N],cnt[N];
int n,m,idx;
inline int read(){
	int ans=0,j=1;char c=getchar();
	while(c>'9' or c<'0'){
        if(c=='-')j=-1;
        c=getchar();
	}
	while(c>='0' and c<='9'){
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans*j;
}
inline void add(int x,int y,int z){
    ver[++idx]=y;
	w[idx]=z;
    nxt[idx]=he[x];
	he[x]=idx;
}
inline bool spfa(){
    queue<int>q;
   	memset(dis,0x3f,sizeof dis);
   	for(int i=1;i<=n;i++){
        q.push(i);
        st[i]=1;
        dis[i]=0;
    }
   	while(!q. empty()){
      	int x=q.front();
		q.pop();
        st[x]=0;
      	for(int i=he[x];i!=0;i=nxt[i]){
            int y=ver[i];
            if(dis[y]>dis[x]+w[i]){
                dis[y]=dis[x]+w[i];
                cnt[y]=cnt[x]+1;
                if(cnt[y]>n) return 0;
                if(!st[y]){
                    st[y]=1;
                    q.push(y);
				}
       		}
        }
    }
   	return 1;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int t;
	t=read();
	while(t--){
		n=read();
    	m=read();
   		for(int i=1;i<=m;i++){
	        int x,y,z;
			x=read();
			y=read();
			z=read();
	        add(x,y,z);
	    }
	   	if(spfa()==0) cout<<"YES"<<endl;
	   	else cout<<"NO"<<endl;
	}
    
   	return 0;
}
``

回复

0 条回复,欢迎继续交流。

正在加载回复...