P3385测试点#9WA,请问是哪里写错了?
查看原帖
P3385测试点#9WA,请问是哪里写错了?
1149120
nbforever楼主2024/4/23 16:00
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
struct edge {
	int to,len;
};
int t,m,n,u,v,w,vis[2001],head,cur,l;
long long dis[2001],cnt[2001];
vector<edge> graph[2001];
queue<int> q;
bool path() {
	memset(dis,127,sizeof dis);
	memset(vis,0,sizeof vis);
	memset(cnt,0,sizeof cnt);
	dis[1]=0;
	vis[1]=1;
	q.push(1);
	while(!q.empty()) {
		head=q.front();
		q.pop();
		vis[head]=0;
		for(int i=0;i<graph[head].size();i++) {
			cur=graph[head][i].to;
			l=graph[head][i].len;
			if(dis[cur]>dis[head]+l) {
				dis[cur]=dis[head]+l;
				cnt[cur]=cnt[head]+1;
				if(cnt[cur]>=n) {
					return true;
				}
				if(!vis[cur]) {
					q.push(cur);
					vis[cur]=1;
				}
			}
		}
	}
	return false;
}
int main() {
	cin>>t;
	for(int i=1;i<=t;i++) {
		cin>>n>>m;
		for(int j=1;j<=m;j++) {
			cin>>u>>v>>w;
			graph[u].push_back((edge){v,w});
			if(w>0) {
				graph[v].push_back((edge){u,w});
			}
		}
		if(path()) {
			cout<<"YES"<<endl;
		}
		else {
			cout<<"NO"<<endl;
		}
		for(int j=1;j<=n;j++) {
			graph[j].clear();
		}
	}
	return 0;
}

附上提交记录链接:R156818940

2024/4/23 16:00
加载中...