题目:
思路:
如果从1开始到x1,和x2的距离(x1,和x2和y相连)相同。那么1->y的最短路的数目=1->x1的最短路数目+1->x2的最短路数目
代码说明:
dis[i]表示1-i的最短路的距离;
AC代码:
#include<cstdio>
#include <iostream>
#include<cstring>
#include<algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=1000011;
const int mod=100003;
vector <int> e[2*maxn];
queue<int> q;
int n,m,dis[maxn],ans[maxn];
bool vis[maxn];
int main()
{
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
vis[1]=1;
ans[1]=1;
q.push(1);
while(!q.empty())
{
int temp1=q.front();
q.pop();
for(int i=0;i<e[temp1].size();i++)
{
int temp2=e[temp1][i];
if(!vis[temp2])
{
dis[temp2]=dis[temp1]+1;
vis[temp2]=1;
q.push(temp2);
}
if(dis[temp2]==dis[temp1]+1)
{
ans[temp2]=(ans[temp2]+ans[temp1])%mod;
//cout<<"temp2="<<temp2<<" "<<"ans[temp2]= "<<ans[temp2]<<endl;
}
}
}
for(int i=1;i<=n;i++) printf("%d\n", ans[i]);
return 0;
}