P1144 最短路计数(如何进行最短路计数?)

 给定一个无向图,我们要得到从一号点到任何点的最短路数字是多少,那么我们能怎么做呢?

  答案就是将,ans在里面进行更新。

  怎么更新呢?我们来思考spfa算法每次是怎么进行更新的,spfa算法每一次的更新都是基于当前的t点能够带给更小的价值来达到这个点,于是我们有了dist[j]=dist[t]+w[i]代表着到第j个点的距离,由原来存储的最小值更新为了到t这个点的最小值加上w[i]的值。

  发现什么了没有我们的每一次距离更新都是与已经确定了最小值的点有关的,那么我们怎么才能将ans融入进去呢?

  答案也很简单,ans[j]=ans[t]这样我们就得到了到j的最短路是由t带给她的,但是仅仅这样还是不够的,因为我们存储的是最小路径长度所以我们很容易想到当dist[j]==dist[t]+1时,ans[j]+=ans[t]这样也是一重更新。

  当然最最最关键的点来了,我们在初始化距离为0时别忘了将ans[1]初始为1,道理与距离的更新相同不是吗?

  接下来代码展示。

  

#include <bits/stdc++.h>
using namespace std;
//数据范围大的时候请用邻接表来计算,并且也正因为这么大的数据所以采用spfa或者堆优化的dijkstra。
const int N = 1e6 + 10;
const int M = 2e6 + 10;
int h[N];
int e[M];
int ne[M];
int ans[N];
int dist[N];
int idx;
bool st[N];
int n, m;
int mod = 100003;

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void spfa() {
	memset(dist, 0x3f, sizeof(dist));
	queue<int>q;
	dist[1] = 0;
	ans[1] = 1;
	q.push(1);
	st[1] = true;
	while (q.size()) {
		auto t = q.front();
		q.pop();
		st[t] = false;
		for (int i = h[t]; i != -1; i = ne[i]) {
			int j = e[i];
			if (dist[j] > dist[t] + 1) {
				ans[j] = ans[t];
				dist[j] = dist[t] + 1;
				if (!st[j]) {
					st[j] = true;
					q.push(j);
				}
			} else if (dist[j] == dist[t] + 1) {
				ans[j] += ans[t];
				ans[j] %= mod;
			}
		}
	}
}

int main() {
	memset(h, -1, sizeof(h)); //每一个邻接表都要记得把h初始化为-1,否则会受到打击。
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		add(b, a); //无向图注意两条边。
	}
	spfa();
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << "\n";
	}
}

下面是迪姐克斯拉算法的解决:

#include <bits/stdc++.h>
using namespace std;
//数据范围大的时候请用邻接表来计算,并且也正因为这么大的数据所以采用spfa或者堆优化的dijkstra。
const int N = 1e6 + 10;
const int M = 2e6 + 10;
int h[N];
int e[M];
int ne[M];
int ans[N];
int dist[N];
int idx;
bool st[N];
int n, m;
int mod = 100003;
typedef pair<int, int>PII;

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dijskra() {
	memset(dist, 0x3f, sizeof(dist));
	dist[1] = 0;
	ans[1] = 1;
	priority_queue<PII, vector<PII>, greater<PII>> hh;
	hh.push({0, 1});
	while (!hh.empty()) {
		auto t = hh.top();
		hh.pop();
		int k = t.second;
		int d = t.first;
		if (st[k])
			continue;
		st[k] = true;
		for (int i = h[k]; i != -1; i = ne[i]) {
			int j = e[i];
			if (dist[j] > dist[k] + 1) {
				dist[j] = dist[k] + 1;
				ans[j] = ans[k];
				hh.push({dist[j], j});//注意插入优先队列的操作别忘了。
			} else if (dist[j] == dist[k] + 1) {
				ans[j] += ans[k];
				ans[j] %= mod;
			}
		}
	}
}


int main() {
	memset(h, -1, sizeof(h)); //每一个邻接表都要记得把h初始化为-1,否则会受到打击。
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		add(b, a); //无向图注意两条边。
	}
	dijskra();
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << "\n";
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值