最短路条数计数

给一篇博客链接:http://tzdyy.lofter.com/post/1e3cd119_10c53ec2
题目:洛谷 P1144 最短路计数
主要是定义了两个数组,一个是f[i]数组,表示i这个点能够提供的方案,一个是sf[i]数组,表示的是到i这个点的方案数。(因为普通的加法原理不好搞定,学长说的)分几种情况,具体看代码中的解释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<deque>
using namespace std;
const int mod=100003;
struct arr{
    int nd,nx,co;
}bot[2500000];
int head[1500000],SF[1500000],f[1500000],dist[1500000],vis[1500000];
int n,m,cnt;
inline int read(){
    int x=0,w=1;char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') w=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar();
    return x*w;
}
inline void add(int a,int b,int c) { bot[++cnt].nd=b; bot[cnt].nx=head[a]; bot[cnt].co=c; head[a]=cnt; }
inline void SPFA(int s){
    deque<int>q;
    for(register int i=1;i<=n;++i) dist[i]=0x3f3f3f3f,vis[i]=0;
    dist[s]=0;vis[s]=1;q.push_back(s);
    while(!q.empty()) {
        int u=q.front();int v; q.pop_front();
        vis[u]=0;
        for(register int i=head[u];i;i=bot[i].nx) { 
            if(dist[v=bot[i].nd]==dist[u]+bot[i].co){
            //如果距离相等,那么就要看当前点v是否在队列中,如果在的话,就累加,不在的话,直接赋值。
                if(vis[v])  f[v]+=f[u],f[v]%=mod;
                else f[v]=f[u];
  //最后sf数组要更新,因为是距离相等的情况,所以能够到v这个点的路径方案数要累加
                SF[v]+=SF[u];SF[v]%=mod;
            }
            if(dist[v=bot[i].nd]>dist[u]+bot[i].co){
                dist[v]=dist[u]+bot[i].co;
                f[v]=f[u];
                SF[v]=SF[u];
                //因为最短路更新了,所以f和sf数组直接更新
                if(!vis[v]){
                    vis[v]=1;
                    if(q.empty()||dist[v]>dist[q.front()]) q.push_back(v);
                    else q.push_front(v);
                }
            }
        }
    }
}
int main(){
    n=read();m=read();
    for(register int i=1;i<=m;++i) {
        int u=read(),v=read();
        add(u,v,1);add(v,u,1);
    }
    f[1]=1;SF[1]=1;
    SPFA(1);
    for(register int i=1;i<=n;++i) printf("%d\n",SF[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值