洛谷日刷p1144 | Eternal_zttz

洛谷日刷p1144

P1144 最短路计数

洛谷传送门

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#define M 1000005
#define N 100005
#define INF 99999999
#define mod 100003
using namespace std;
struct node {
int u,v,next;
};
int head[N],len[N],ans[N];
bool vis[N];
int n,m,cnt,s = 1;
void add(node edge[],int u,int v){
edge[cnt]={u,v,head[u]};
head[u] = cnt++;
}
void SPFA(queue<int>q,node edge[]){
q.push(s);
vis[s] = true;
for (int i=1;i<=n;i++)
len[i] = INF;
len[s] = 0;
ans[s] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u];i>0;i=edge[i].next){
int v = edge[i].v;
if(len[v]> len[u]+1){
len[v] =len[u]+1;
ans[v] = ans[u];
if(!vis[v]){
vis[v] =true;
q.push(v);
}
}
else if(len[v] == len[u]+1){
ans[v] += ans[u];
ans[v] %= mod;
}
}
}
}
int main(){
scanf("%d %d",&n,&m);
node edge[2*m+1];
cnt = 1;
memset(head,0, sizeof(head));
memset(len,0,sizeof(len));
memset(vis,false,sizeof(vis));
queue<int>q;
for (int i =1;i<=m;i++){
int u,v;
scanf("%d %d",&u,&v);
add(edge,u,v);
add(edge,v,u);
}
SPFA(q, edge);
for (int i =1;i<=n;i++)
printf("%d\n",ans[i]);
}

-------------The End-------------