题目链接:
https://www.luogu.org/problemnew/show/P1144
题意:
给定一个 n n n个点 m m m条边的无向图(注意是无向图),求从点 1 1 1到点 i i i的最短路径的条数,每行一个,共n行;
分析:
开一个数组
a
n
s
[
i
]
ans[i]
ans[i],记录
i
i
i点的最短路径条数,如果对于
(
u
,
v
)
(u,v)
(u,v)能够更新,那么就使
a
n
s
[
v
]
=
a
n
s
[
u
]
ans[v]=ans[u]
ans[v]=ans[u],如果
u
,
v
u,v
u,v两点的最短路径相等,那么就
a
n
s
[
v
]
+
=
a
n
s
[
u
]
ans[v]+=ans[u]
ans[v]+=ans[u]就可以了;
这里用上了堆优化的dijkstra,复杂度
O
(
(
n
+
m
)
l
o
g
n
)
O((n+m)logn)
O((n+m)logn) ;用斐波那契堆可以降到
O
(
m
+
n
l
o
g
n
)
O(m+nlogn)
O(m+nlogn)
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define inf 0x7f7f7f7f
#define maxn 10011
#define N 1000500
#define mod 100003
typedef long long ll;
typedef struct{
int u,v,next,w;
}Edge;
Edge e[2*N];
int cnt,head[N];
inline void add(int u,int v){
e[cnt].u=u;
e[cnt].v=v;
//e[cnt].w=w;
// e[cnt].f=f;
e[cnt].next=head[u];
head[u]=cnt++;
//e[cnt].u=v;
// e[cnt].v=u;
// e[cnt].w=0;
// e[cnt].f=-f;
// e[cnt].next=head[v];
// head[v]=cnt++;
}
inline void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
}
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
//没写完的splay = =
/*struct splay_Tree{
int ch[N][2],f[N],cnt[N],size[N],n,root,key[N];
splay_Tree(){
memset(ch,0,sizeof(ch));
memset(f,0,sizeof(f));
memset(cnt,0,sizeof(cnt));
memset(size,0,sizeof(size));
n=0;
}
int get(int x) {
return ch[f[x]][1]==x;
}
void update(int x) {
if(x){
size[x]=cnt[x];
if(ch[x][0])size[x]+=size[ch[x][0]];
if(ch[x][1])size[x]+=size[ch[x][1]];
}
}
void rotate(int x){
int y=f[x],z=f[y];
int kind=get(x);
ch[y][kind]=ch[x][!kind],f[ch[y][kind]]=y;
ch[x][!kind]=y,f[y]=x;
if(z){
ch[z][ch[z][1]==y]=x;
}
//更新顺序不能反
update(y),update(x);
}
void splay(int x) {
for (int fa; fa = f[x]; rotate(x)) {
if (f[fa])
rotate((get(x) == get(fa) ? fa : x));
}
root = x;
}
void insert(int x) {
if(root==0){
n++;
size[n]=cnt[n]=1;
ch[n][0]=ch[n][1]=f[n]=0;
root=n;key[n]=x;
return ;
}
int now=root,fa=0;
while(1){
if(key[now]==x){
cnt[now]++;
update(now);
update(fa);
splay(now);
return ;
}
fa=now;
now=ch[now][key[now]<x];
if(now==0){
n++;
size[n]=cnt[n]=1;
ch[n][0]=ch[n][1]=f[n]=0;
root=n,key[n]=x;
ch[fa][key[fa]<x],f[now]=fa;
update(fa);
splay(n);
return ;
}
}
}
};*/
int n,m,dis[N],ans[N],vis[N];
void dijkstra(int x){
memset(dis,inf,sizeof(dis));
dis[x]=0;
priority_queue<pair<int,int > >q;
q.push(make_pair(0,1));
ans[1]=1;
while(!q.empty()){
int u=q.top().second;q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
ans[v]=ans[u]%mod;
q.push(make_pair(-dis[v],v));
}
else if(dis[v]==dis[u]+1)
ans[v]=(ans[v]+ans[u])%mod;
}
}
}
int main() {
memset(head,-1,sizeof(head));
cin>>n>>m;
int a,b;
for(int i=0;i<m;i++){
a=read(),b=read();
add(a,b);
add(b,a);
}
dijkstra(1);
for(int i=1;i<=n;i++){
cout<<ans[i]<<endl;
}
//system("pause");
return 0;
}
(仅供个人理解)