码迷,mamicode.com
首页 > 其他好文 > 详细

P1144 最短路计数

时间:2019-11-05 13:55:11      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:png   地址   stream   signed   需要   nod   http   mic   The   

技术图片

 技术图片

题目地址


注意点:

  • 本题需要开双向边.

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define int long long
using namespace std;
const int MAXN=2e6,MAXM=4e6,INF=2e9,MOD=100003;
struct Edge{
	int from,to,w,nxt;
}e[MAXM];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v,int w){
	e[++edgeCnt].from=u;
	e[edgeCnt].to=v;
	e[edgeCnt].w=w;
	e[edgeCnt].nxt=head[u];
	head[u]=edgeCnt;
}
struct Node{
	int v,dis;
	bool operator <(Node another)const{
		return dis>another.dis;
	}
};
int dis[MAXN],cnt[MAXN];
int s=1;
void dijkstra(){
	memset(dis,0x3f,sizeof(dis));
	priority_queue<Node> q;
	q.push(Node{s,0});
	dis[s]=0,cnt[s]=1;
	while(!q.empty()){
		Node nowNode=q.top();
		q.pop();
		int nowU=nowNode.v;
		for(int i=head[nowU];i;i=e[i].nxt){
			int nowV=e[i].to;
			if(dis[nowV]>dis[nowU]+e[i].w){
				dis[nowV]=dis[nowU]+e[i].w;
				cnt[nowV]=cnt[nowU];
				q.push(Node{nowV,dis[nowV]});
			}else if(dis[nowV]==dis[nowU]+e[i].w){
				cnt[nowV]+=cnt[nowU];
				cnt[nowV]=cnt[nowV]%MOD;
			}
		}
	}
}
signed main(){
	int n,m;
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y;
		scanf("%lld%lld",&x,&y);
		addEdge(x,y,1);
		addEdge(y,x,1);
	}
	
	dijkstra();
	
	for(int i=1;i<=n;i++){
		cnt[i]=cnt[i]%MOD;
		printf("%lld\n",cnt[i]);
	}
	
	return 0;
}

  


P1144 最短路计数

标签:png   地址   stream   signed   需要   nod   http   mic   The   

原文地址:https://www.cnblogs.com/zbsy-wwx/p/11797581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!