求条玄关
查看原帖
求条玄关
1234924
lsd110504lsd楼主2025/8/5 15:19

样利不过

//xiaogu orz%%%
#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
#define int unsigned long long
//using namespace __gnu_pbds;
int n,m,s;
const int N=2e5+520;
vector<pair<int,int> > G[N];
int dist[N];
bool vis[N];
inline void Dij(){
	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
	q.push({0,s});
	memset(dist,0x3f3f3f3f,sizeof(dist));
	dist[s]=0;
	while(!q.empty()){
		auto u=q.top();
		q.pop();
		if(vis[u.second])	continue;
		vis[u.second]=1;
		for(auto v:G[u.second]){
			if(dist[u.second]+v.second<dist[v.first]){
				dist[v.first]=dist[u.second]+v.second;
				q.push({dist[v.first],v.first});
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout<<dist[i]<<" ";
	}
}
int cnt[N];
inline void spfa(){
	queue<int> q;
	memset(dist,0x3f3f3f3f,sizeof(dist));
	memset(vis,0,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	q.push(1);
	dist[1]=0;
	vis[1]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=false;
		for(auto v:G[u]){
			if(dist[u]+v.second<dist[v.first]){
				dist[v.first]=dist[u]+v.second;
				cnt[v.first]=max(cnt[v.first],cnt[u]+1);
				if(cnt[v.first]>=n){
					cout<<"YES"<<endl;
					return ;
				}
				if(!vis[v.first]){
					q.push(v.first);
					vis[v.first]=1;
				}
			}
		}
	}
	cout<<"NO"<<endl;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int u,v,w;
			cin>>u>>v>>w;
			if(w>=0)
				G[v].push_back({u,w}),G[u].push_back({v,w});
			else 
				G[u].push_back({v,w});
		}
		spfa();
		for(int i=1;i<=n;i++)	G[i].clear();
	//	Dij();
		for (int i = 1;i <= n;i ++) {
			cerr << cnt[i] << ' ';
		}
	}
	return 0;
}
2025/8/5 15:19
加载中...