【BZOJ3875】[Ahoi2014]骑士游戏【最短路】【DP】

【题目链接】

对于怪物u,普通攻击打死后产生的怪物为vi。设dis[u]表示打死u的最小花费,那么有

dis[u] = min{s[u] + ∑dis[vi], k[u]}

以这个为松弛条件,跑spfa就可以啦。


然而BZOJ跑了29s...变为倒数rank1

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;

const int maxn = 200005, maxm = 1000005, maxq = 200005;

int n, head[maxn], cnt, q[maxq];
ULL s[maxn], k[maxn], dis[maxn];
bool vis[maxn];

struct _edge {
	int v, next;
} g[maxm << 1];

template <class nt>
inline void read(nt &x) {
	bool f = 0; x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? 1 : 0;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	if(f) x = -x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

inline void spfa() {
	int h = 0, t = 0;
	for(int i = 1; i <= n; i++) dis[i] = k[i], vis[q[t++] = i] = 1;
	while(h != t) {
		int u = q[h]; vis[u] = 0; h == maxq - 1 ? h = 0 : h++;
		ULL res = s[u];
		for(int i = head[u]; ~i; i = g[i].next) if(~i & 1) res += dis[g[i].v];
		if(res < dis[u]) {
			dis[u] = res;
			for(int i = head[u]; ~i; i = g[i].next) if(i & 1) if(!vis[g[i].v]) {
				q[t] = g[i].v;
				t == maxq - 1 ? t = 0 : t++;
			}
		}
	}
}

int main() {
	read(n);
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;

	for(int i = 1, x; i <= n; i++) {
		read(s[i]); read(k[i]);
		for(read(x); x; x--) {
			int u; read(u);
			add(i, u); add(u, i);
		}
	}

	spfa();

	printf("%llu\n", dis[1]);
	return 0;
}


你可能感兴趣的:(【BZOJ3875】[Ahoi2014]骑士游戏【最短路】【DP】)