p1144 最短路计数

在优化版的 迪杰斯特拉 加入计数数组;

所属题目
P1144 最短路计数
评测状态
Accepted
评测分数
100

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=2e6+5;
typedef pair<int,int>PII;
priority_queue<PII,vector<PII>,greater<PII> >heap;

int n,m;
int dist[N],ans[N];
bool st[N];
int mod=100003;
int h[N],ne[N],e[N],idx;
void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dijstra()
{
    memset(dist,0x3f,sizeof(dist));
    dist[1]=0;
    ans[1]=1;
    heap.push({0,1});
    while(!heap.empty())
    {
        PII t=heap.top();
        heap.pop();
        int a=t.second,b=t.first;
	 	if(st[a]) continue;
	    st[a]=true;
	    
	    for(int i=h[a];i!=-1;i=ne[i])
	    {
	        int j=e[i];
	        if(dist[j]>b+1)
	        {
	            dist[j]=b+1;
	            ans[j]=ans[a];
	            heap.push({dist[j],j});
	        }
	        else if(dist[j]==b+1)
	        {
	            ans[j]+=ans[a];
	            ans[j]%=mod;
	        }
	    }
     }
}

//if(dist[n]==0x3f3f3f3f) return -1;
//return dist[n];

}
int main()
{
    memset(h,-1,sizeof(h));
    cin >>n >>m;
    while(m--)
    {
        int x,y;
        cin >>x>>y;
        add(x,y);
        add(y,x);
    }
    dijstra();
    for(int i=1;i<=n;i++)
    {
        cout <<ans[i]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值