为什么memset(dis,100001,sizeof(dis))是不可行的?
查看原帖
为什么memset(dis,100001,sizeof(dis))是不可行的?
800543
NirvanaCeleste楼主2024/6/22 18:32
#include <bits/stdc++.h>
using namespace std;
const int pll = 100001,M = 3030 * 2,N = 2020;
inline void read(int& qwq){
	int awa = 0,w = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){
		if(ch == '-') w = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9'){
		awa = awa * 10 + ch - '0';
		ch = getchar();
	}
	qwq = awa * w;
}
int n,m,tot,T;
struct node{
	int to,vlu,nxt;
}G[M];
int hed[N],dis[N],cnt[N];
bool pd;
bool vis[N];
void insert_edge(int u,int v,int w){
	G[tot].vlu = w;
	G[tot].to = v;
	G[tot].nxt = hed[u];
	hed[u] = tot++;
}
void pre(){
	memset(vis,0,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	memset(dis,100001,sizeof(dis));
	memset(hed,-1,sizeof(hed));
	tot = 0;
	pd = 0;
}
void scan(){
	read(n),read(m);
	int u,v,w;
	for(int i=1;i<=m;i++){
		read(u),read(v),read(w);
		if(w >= 0) insert_edge(u,v,w),insert_edge(v,u,w);
		else insert_edge(u,v,w);
	}
}
void spfa(int s){
	queue<int> sp;
	dis[s] = 0;
	vis[s] = 1;
	cnt[s]++;
	sp.push(s);
	while(!sp.empty()){
//		for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
//		cout<<endl;
		if(pd) break;
		int u = sp.front();
		sp.pop();
		vis[u] = 0;
		for(int i=hed[u];i;i=G[i].nxt){
			int v = G[i].to,w = G[i].vlu;
			if(dis[v] > dis[u] + w){
				dis[v] = dis[u] + w;
				if(!vis[v]){
					sp.push(v);
					vis[v] = 1;
					if(++cnt[v] > n) pd = 1;
				}
			}
		}
	}
}
int main(){
	cin>>T;
	for(int i=1;i<=T;i++){
		pre();
		scan();
//		for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
//		cout<<endl;
		spfa(1);
		if(pd) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}
2024/6/22 18:32
加载中...