在优化版的 迪杰斯特拉 加入计数数组;
所属题目
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;
}