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

最短路计数 [LuoGu P 1144]

时间:2017-11-03 20:24:16      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:link   for   false   iostream   while   continue   logs   span   front   

题目大意 : 

给出一个N个顶点M条边的无向无权图,顶点编号为1~N。问从顶点1开始,到其他每个点的最短路有几条。

 

大水题 , 为了备忘 , 还是记下来吧 .

 

不推方程了,,,,in Code .

 

技术分享
  1 //Copyright(C)Sunshine.
  2 //2017.11.03
  3 //*Dijkstra:
  4 #include<cstdio>
  5 #include<cstring>
  6 #include<queue>
  7 #include<vector>
  8 using namespace std;
  9 const int INF=2147483647;
 10 const int MAXN=1e6+1,MOD=100003;
 11 int SP[MAXN],cnt[MAXN];
 12 struct Edge
 13 {
 14     int to,dis;
 15     Edge(int t=0,int d=INF):to(t),dis(d){}
 16 };
 17 vector<Edge>E;
 18 vector<int>G[MAXN];
 19 struct HeapNode
 20 {
 21     int u,dis;
 22     HeapNode(int x=0,int d=INF):u(x),dis(d){}
 23     bool operator < (const HeapNode& r) const {return this->dis>r.dis;}
 24 };
 25 priority_queue<HeapNode>Q;
 26 inline int read()
 27 {
 28     int f=1,x=0;char c=getchar();
 29     while(c<0||c>9){if(c==-)f=-1;c=getchar();}
 30     while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
 31     return f*x;
 32 }
 33 void insert(int from,int to,int dis)
 34 {
 35     E.push_back(Edge(to,dis));
 36     G[from].push_back(E.size()-1);
 37 }
 38 int main()
 39 {
 40     int n=read(),m=read();
 41     for(int i=1;i<=m;i++)
 42     {
 43         int x=read(),y=read();
 44         insert(x,y,1);
 45         insert(y,x,1);
 46     }
 47     for(int i=0;i<=n+1;i++)SP[i]=INF;
 48     SP[1]=0;cnt[1]=1;
 49     Q.push(HeapNode(1,SP[1]));
 50     while(!Q.empty())
 51     {
 52         HeapNode a=Q.top();Q.pop();
 53         if(a.dis!=SP[a.u])continue;
 54         int siz=G[a.u].size();
 55         for(int i=0;i<siz;i++)
 56         {
 57             Edge it=E[G[a.u][i]];
 58             if(SP[it.to]>SP[a.u]+it.dis)
 59             {
 60                 cnt[it.to]=cnt[a.u];
 61                 SP[it.to]=SP[a.u]+it.dis;
 62                 Q.push(HeapNode(it.to,SP[it.to]));
 63             }
 64             else if(SP[it.to]==SP[a.u]+1)
 65             cnt[it.to]=(cnt[it.to]+cnt[a.u])%MOD; 
 66         }
 67     }
 68     for(int i=1;i<=n;i++)
 69         if(SP[i]<INF)printf("%d%c",cnt[i],10);
 70         else printf("%d%c",0,10);
 71     return 0;
 72 }
 73 //*SPFA:
 74 //#include<cstdio>
 75 //#include<cstring>
 76 //#include<queue>
 77 //#include<iostream>
 78 //using namespace std;
 79 //const int N=1e6+1,M=2e6+1;
 80 //const int mod=100003,INF=1e7+1,dis=1;
 81 //int h[N],cnt[N],sp[N],Ect=0;
 82 //bool vis[N];
 83 //struct Graph{
 84 //    int to,link;
 85 //    Graph(){to=link=0;}
 86 //};
 87 //Graph G[M];
 88 //queue<int>q;
 89 //
 90 //void AddEdge(int from,int to)
 91 //{
 92 //    Ect++;
 93 //    G[Ect].link=h[from];
 94 //    G[Ect].to=to;
 95 //    h[from]=Ect;
 96 //}
 97 //inline int read()
 98 //{
 99 //     char c=getchar();int f=1,s=0;
100 //     while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
101 //     while(c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘;c=getchar();}
102 //     return s*f;
103 //}
104 //int main()
105 //{
106 //    for(int i=0;i<N;i++)sp[i]=INF;
107 //    memset(cnt,0,sizeof(cnt));
108 //    const int begin=1;
109 //    int n=read(),m=read();
110 //    for(int i=1,x,y;i<=m;i++)
111 //    {
112 //        x=read();y=read();
113 //        AddEdge(x,y);AddEdge(y,x);
114 //    }
115 //    int now=0;
116 //    sp[begin]=0;
117 //    q.push(begin);
118 //    vis[begin]=true;
119 //    cnt[begin]=1;
120 //    while(!q.empty())
121 //    {
122 //        now=q.front();
123 //        q.pop();
124 //        vis[now]=false;
125 //        for(int it=h[now];it!=0;it=G[it].link)
126 //            if(sp[now]+dis==sp[G[it].to])
127 //            {
128 //                cnt[G[it].to]+=cnt[now];
129 //                cnt[G[it].to]%=mod;
130 //            }
131 //            else if(sp[now]+dis<sp[G[it].to])
132 //            {
133 //                cnt[G[it].to]=cnt[now];
134 //                sp[G[it].to]=sp[now]+dis;
135 //                 if(!vis[G[it].to])
136 //                {
137 //                    q.push(G[it].to);
138 //                    vis[G[it].to]=true;
139 //                }
140 //            }
141 //        }
142 //    for(int i=1;i<=n;i++)
143 //        printf("%d\n",cnt[i]);
144 //    return 0;
145 //}
Dijkstra & SPFA

 

最短路计数 [LuoGu P 1144]

标签:link   for   false   iostream   while   continue   logs   span   front   

原文地址:http://www.cnblogs.com/zhinv/p/7779983.html

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