60分求调
查看原帖
60分求调
1064275
kmltz楼主2024/7/20 19:46
using namespace std;
#include<bits/stdc++.h>
#define ll long long
const ll P=100003;
const ll N=1e6+7;
const ll INF=1e18+7;
ll read(){
	
	ll f=1,summ=0;char ch;
	ch=getchar();
	while(ch<'0'|ch>'9'){
		if(ch=='-'){
			f=-1;
		}ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		summ=summ*10+ch-'0';
		ch=getchar();
		
	}
	return summ*f;
} 
ll head[N];ll cnt=0;
struct ee{
	ll to,nxt,w;
}ed[N<<1];
struct node{
	ll dis,pos;
	bool operator <(const node &x)const{
		return x.dis<dis;
	
	}
};
priority_queue<node> q;
void add_t(ll u,ll v,ll w){
	ed[++cnt].nxt=head[u];
	ed[cnt].to=v;
	ed[cnt].w=w;
	head[u]=cnt;

}
ll n,m,s;
ll dis[N];
ll cntt[N];ll vis[N];
void dijkstra(){
	q.push((node{dis[s],s}));
	while(!q.empty()){
		node t=q.top();
		ll id=t.pos;
		q.pop();
		if(vis[id]) continue;
		vis[id]=1;
		ll vv;
		ll ww;
		for(int i=head[id];i;i=ed[i].nxt){
			vv=ed[i].to;ww=ed[i].w;
			if(dis[vv]>dis[id]+ww){
				dis[vv]=dis[id]+ww;
				cntt[vv]=cntt[id];
				 if( !vis[vv] )
                {
                    q.push( ( node ){dis[vv], vv} );
                }
				
			}else if(dis[vv]==dis[id]+ww){
				cntt[vv]+=cntt[id];
			}
			
		}
		
	}
	
}
int main(){
	n=read();m=read();s=1;
	memset(dis,0x3f,sizeof(dis));
	for(int i=1;i<=m;i++){
		ll u,v;
		u=read();v=read();
		add_t(u,v,1);
		add_t(v,u,1); 
	}
	dis[s]=0;
	cntt[s]=1;
	dijkstra();
	for(int i=1;i<=n;i++){
		printf("%d\n",cntt[i]);
	}
	return 0;
}
2024/7/20 19:46
加载中...