社区讨论

60pts MLE+TLE求调

P1144最短路计数参与者 2已保存回复 4

讨论操作

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

当前回复
4 条
当前快照
1 份
快照标识符
@mke23o85
此快照首次捕获于
2026/01/14 21:28
2 个月前
此快照最后确认于
2026/01/17 21:45
2 个月前
查看原帖
代码
CPP
//P1144 最短路统计 
//难度:普及/提高-  考点:最短路 
#include<bits/stdc++.h>
#define inf 2e9
#define maxn 1000010
#define maxm 2000010
using namespace std;

struct edge{
	int to,next;
}a[maxm];
int head[maxn],cnt;
void add(int from,int to){
	cnt++;
	a[cnt].to=to;
	a[cnt].next=head[from];
	head[from]=cnt;
}

struct node{
	int id,len;
};
queue<node>q;
int n,m;
int dist[maxn];
int ways[maxn]; 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		add(u,v);
		add(v,u);
	}
	for(int i=2;i<=n;i++)
		dist[i]=inf;
	ways[1]=1;
	node cur;
	int id,len;
	q.push({1,0});
	while(q.size()){
		cur=q.front();q.pop();
		id=cur.id;len=cur.len;
		for(int i=head[id];i;i=a[i].next){
			int tp=a[i].to;
			if(len<dist[tp]){
				ways[tp]++;
				dist[tp]=len+1;
				q.push({tp,len+1});
			}
		}
	}
	for(int i=1;i<=n;i++){
		cout<<ways[i]%100003<<'\n';
	}
}

回复

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

正在加载回复...