ac了,但是求助大佬!
查看原帖
ac了,但是求助大佬!
902704
wjx38223楼主2023/9/10 20:39

在我的第19行的初始化里,如果用1e9输出除了第一行都是0,但是用更小的(比如1e4)就能a,萌新不懂,求助大佬!

#include<bits/stdc++.h>
using namespace std;
vector<int> v[1000010];
bool vis[1000010];
int dis[1000010];
int cnt[1000010];
int n, m;
int main(){
	cin >> n >> m;
	for(int i = 1;i<=m; i++){
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	} 
	queue<int> q;
	q.push(1);
	vis[1] = true;
	memset(dis,1e4,sizeof(dis));//这样写是能AC的!
	dis[1] = 0;
	cnt[1] = 1;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		for(int i = 0; i<v[x].size(); i++){
			int to = v[x][i];
			if(dis[x]+1<dis[to]){
				cnt[to] = cnt[x];
				dis[to] = dis[x]+1;
				if(!vis[to]){
					vis[to] = true;
					q.push(to);
				}
			}else if(dis[x]+1==dis[to]){
				cnt[to]=(cnt[to]+cnt[x])%100003;
			}
		}
	}
	for(int i = 1; i<=n; i++){
		cout << cnt[i] << endl;
	}
	return 0;
}

(感谢大佬,悬关)

2023/9/10 20:39
加载中...