题目链接
spfa跑单源最短路,注意相等时线路数相加。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=1e6+10,mod=1e5+3; int n,m,hd[N],tot,dis[N],vis[N],cnt[N]; struct Edge{ int v,nx; }e[N<<2]; inline void add(int u,int v) { e[tot].v=v; e[tot].nx=hd[u]; hd[u]=tot++; } inline void spfa() { memset(dis,0x3f,sizeof(dis)); queue<int>q;dis[1]=0;vis[1]=1;q.push(1);cnt[1]=1; while(q.size()) { int u=q.front();q.pop();vis[u]=0; for(int i=hd[u];~i;i=e[i].nx) { int v=e[i].v; if(dis[v]>dis[u]+1) { dis[v]=dis[u]+1;cnt[v]=cnt[u]; if(!vis[v])q.push(v),vis[v]=1; } else if(dis[v]==dis[u]+1) { cnt[v]+=cnt[u];if(cnt[v]>=mod)cnt[v]-=mod; } } } } int main() { //freopen("in.txt","r",stdin); memset(hd,-1,sizeof(hd)); scanf("%d%d",&n,&m); int u,v; for(int i=1;i<=m;i++) scanf("%d%d",&u,&v),add(u,v),add(v,u); spfa(); for(int i=1;i<=n;i++) printf("%dn",cnt[i]); return 0; }
总结
做逛公园的前置技能
最后
以上就是碧蓝电脑最近收集整理的关于【题解】洛谷P1144最短路计数 spfa的全部内容,更多相关【题解】洛谷P1144最短路计数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复