「CH6202」黑暗城堡

「CH6202」黑暗城堡

传送门
这道题是要让我们求以点 \(1\) 为源点的最短路树的方案数。
我们先跑一遍最短路,然后考虑类似 \(\text{Prim}\) 的过程。
当我们把点 \(x\) 加入当前的生成树 \(T\) 中时,对于 \(\forall p \in T\) ,满足 \(dis_p = dis_x + (x, p)\) 那么就可以把这两个点相连,根据乘法原理,我们把每一步的方案数相乘就是最终的答案。
参考代码:

#include <algorithm>
#include <cstring>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while ('0' > c || c > '9') f |= c == '-', c = getchar();
	while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
	s = f ? -s : s;
}

const int _ = 1010, __ = 5e5 + 5, p = 2147483647;

int n, m, d[_][_], dis[_], vis[_], tag[_];

struct node { int d, id; } t[_];
inline bool cmp(const node& x, const node& y) { return x.d < y.d; }

inline void Dijkstra() {
	memset(dis, 0x3f, sizeof dis);
	dis[1] = 0;
	for (rg int o = 1; o <= n; ++o) {
		int x = 0;
		for (rg int i = 1; i <= n; ++i)
			if (!vis[i] && dis[i] < dis[x]) x = i;
		vis[x] = 1;
		for (rg int i = 1; i <= n; ++i)
			dis[i] = min(dis[i], dis[x] + d[x][i]);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	file("cpp");
#endif
	read(n), read(m);
	memset(d, 0x3f, sizeof d);
	for (rg int u, v, l; m--; )
		read(u), read(v), read(l), d[u][v] = d[v][u] = l;
	Dijkstra();
	for (rg int i = 1; i <= n; ++i) t[i] = (node) { dis[i], i };
	sort(t + 1, t + n + 1, cmp);
	int ans = 1;
	tag[1] = 1;
	for (rg int i = 2; i <= n; ++i) {
		int u = t[i].id, num = 0;
		for (rg int j = 1; j <= n; ++j)
			if (tag[j] && dis[u] == dis[j] + d[j][u]) ++num;
		ans = 1ll * ans * num % p, tag[u] = 1;
	}
	printf("%d\n", ans);
	return 0;
}
posted @ 2020-01-14 10:04  Sangber  阅读(198)  评论(0编辑  收藏  举报