基本思路:正常dijkstra+记录路径
注意:路径求模
代码
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<queue> using namespace std; const int Max_N=1000010,Max_M=2000010,inf=2147432637; typedef pair<int,int>node; struct Edge { int next,dis,to; } E[Max_M*2];//注意是无向图 int Head[Max_N],num_Edge,Dis[Max_N],vis[Max_N],Ans[Max_N]; int N,M; inline int Read(void)//快读 { int w=0,x=0; char ch=0; while(!isdigit(ch)) w|=ch=='-',ch=getchar(); while(isdigit(ch)) x=x*10+(ch^48),ch=getchar(); return w?-x:x; } inline void Add_Edge(int from,int to,int dis)//建边 { E[++num_Edge].dis=dis; E[num_Edge].next =Head[from]; E[num_Edge].to =to; Head[from]=num_Edge; } inline void Dijkstra() { priority_queue< node,vector<node>,greater<node> >Q; for(int i=2; i<=N; ++i) Dis[i]=inf; Ans[1]=1; Q.push(make_pair(0,1)) ; while(!Q.empty()) { int u=Q.top() .second; Q.pop() ; if(vis[u])continue; else vis[u]=1; for(int i=Head[u]; i; i=E[i].next ) { int v=E[i].to; if(Dis[v]==Dis[u]+E[i].dis )//累计最短路条数 Ans[v]=(Ans[v]+Ans[u])%100003; else if(Dis[v]>Dis[u]+E[i].dis )//如果找到更短路,重新统计最短路条数 { Ans[v]=Ans[u]%100003; Dis[v]=Dis[u]+E[i].dis ; if(!vis[v]) Q.push(make_pair(Dis[v],v)) ; } } } } int main(void) { N=Read(),M=Read(); for(int i=1; i<=M; ++i) { int u=Read(),v=Read(); Add_Edge(u,v,1); Add_Edge(v,u,1); } Dijkstra(); for(int i=1; i<=N; ++i) printf("%d\n",Ans[i]%100003);//注意求模 return 0; }