【luogu P1144 最短路计数】 题解

题目链接:https://www.luogu.org/problemnew/show/P1144

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <vector>
 6 using namespace std;
 7 const int mod = 100003;
 8 const int maxn = 2000010;
 9 const int inf = 0x7fffffff;
10 int n, m, dis[maxn], ans[maxn];
11 bool vis[maxn];
12 queue<int>q;
13 vector<int>e[maxn];
14 int SPFA()
15 {
16     while(!q.empty())
17     {
18         int now1 = q.front();
19         q.pop();
20         for(int i = 0; i < e[now1].size(); i++)
21         {
22             int now2 = e[now1][i];
23             if(!vis[now2])
24             {
25                 vis[now2] = 1;
26                 dis[now2] = dis[now1]+1;
27                 q.push(now2);
28             }
29             if(dis[now2] == dis[now1]+1)
30             {
31                 ans[now2] = (ans[now2] + ans[now1])%mod;
32             }
33         }
34     }
35 }
36 int main()
37 {
38     scanf("%d%d",&n,&m);
39     for(int i = 1; i <= m; i++)
40     {
41         int u,v;
42         scanf("%d%d",&u,&v);
43         e[u].push_back(v);
44         e[v].push_back(u);
45     }
46     int s = 1;
47     dis[s] = 0;
48     q.push(s);
49     vis[s] = 1;
50     ans[s] = 1;
51     SPFA();
52     for(int i = 1; i <= n; i++)
53     printf("%d\n",ans[i]);
54 }

 

posted @ 2018-05-04 19:06  Misaka_Azusa  阅读(219)  评论(0编辑  收藏  举报
Live2D