码迷,mamicode.com
首页 > 其他好文 > 详细

P1144 最短路计数

时间:2018-11-08 22:00:24      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:++   har   \n   clu   getc   +=   head   ==   ons   

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1000005;
 4 const int maxm=2000005;
 5 const int INF=1e9;
 6 
 7 inline int read()
 8 {
 9     int x=0,k=1;char c=getchar();
10     while(c<0||c>9) {if(c==-) k=-1;c=getchar();}
11     while(c>=0&&c<=9) x=(x<<3)+(x<<1)+(c^48),c=getchar();
12     return x*k;
13 }
14 
15 int cnt,dis[maxn],vis[maxn],head[maxm],n,m,ans[maxn];
16 
17 struct Edge{
18     int u,v,w,next;
19 }e[maxm];
20 
21 inline void add(int u,int v)
22 {
23     e[++cnt].u=u;
24     e[cnt].v=v;
25     e[cnt].next=head[u];
26     head[u]=cnt;
27 }
28 
29 struct node{
30     int w,now;
31     inline bool operator <(const node& x) const
32     {
33     return w>x.w;
34     }
35 };
36 
37 priority_queue<node> q;
38 
39 void dijikstra()
40 {
41     for(int i=1;i<=n;++i) dis[i]=INF;
42     dis[1]=0;
43     ans[1]=1;
44     q.push((node){0,1});
45     while(!q.empty())
46     {
47         node x=q.top();
48         q.pop();
49         int u=x.now;
50         if(vis[u]) continue;
51         vis[u]=1;
52         for(int i=head[u];i;i=e[i].next)
53         {
54             int v=e[i].v;
55             if(dis[v]>dis[u]+1)
56             {
57                 dis[v]=dis[u]+1;
58                 ans[v]=ans[u];
59                 q.push((node){dis[v],v});
60             }
61             else if(dis[v]==dis[u]+1)
62             {
63                 ans[v]+=ans[u];
64                 ans[v]%=100003;
65             }
66         }
67     }
68 }
69 
70 int main()
71 {
72     n=read(),m=read();
73     for(int i=1,x,y;i<=m;++i)
74     {
75         x=read(),y=read();
76         add(x,y);
77         add(y,x);
78    }
79    dijikstra();
80    for(int i=1;i<=n;++i)
81    {
82        printf("%d\n",ans[i]);
83    }
84    return 0;
85 }

这道题讲真不是很懂

P1144 最短路计数

标签:++   har   \n   clu   getc   +=   head   ==   ons   

原文地址:https://www.cnblogs.com/zytwan/p/9931854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!