最短路计数(模板)

P1144 最短路计数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include<bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int N = 2e6+10;
struct edge
{
	int from,to;
	long long w;
	edge(int a,int b,long long c)
	{
		from = a;
		to = b;
		w = c;
	}
};
vector<edge> E[N]; //存图 
struct node
{
	int id;
	long long n_dis;
	node(int x,long long num)
	{
		id = x;
		n_dis = num;
	}
	bool operator < (const node &a) const
	{
		return n_dis > a.n_dis;
	}
};
int n,m;
int pre[N];
void print_path(int s,int t)
{
	if(s==t)
	{
		cout<<s;
		return;
	}
	print_path(s,pre[t]);
	cout<<t;
}
long long dis[N];
long long ans[N];
bool vis[N];
void dijkstra(int s)
{
	for(int i=1;i<=n;i++)
	{
		dis[i]=INF;
		vis[i]=false;
		ans[i]=0;
	}
	dis[s]=0;
	ans[s]=1;
	priority_queue<node> Q;
	Q.push(node(s,dis[s]));
	while(!Q.empty())
	{
		node u = Q.top();
		Q.pop();
		if(vis[u.id]) continue;
		vis[u.id] = true;
		for(int i=0;i<E[u.id].size();i++)
		{
			edge y = E[u.id][i];
			if(vis[y.to]) continue;
			if(dis[y.to] > y.w+u.n_dis)
			{
				dis[y.to] = y.w + u.n_dis;
				ans[y.to] = ans[y.from];
				Q.push(node(y.to,dis[y.to]));
				pre[y.to] = u.id;
			}else if(dis[y.to] == y.w + u.n_dis)
			{
				ans[y.to] =(ans[y.to]+ans[y.from])%100003; 
			}
		}
	}
}
int main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	int k;
	cin>>n>>m;
	for(int i=1;i<=n;i++) E[i].clear();
	while(m--)
	{
		int u,v,w=1;
		cin>>u>>v;
		E[u].push_back(edge(u,v,w));
		E[v].push_back(edge(v,u,w));
	}
	dijkstra(1);
	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、付费专栏及课程。

余额充值