关闭

【BZOJ1576】[Usaco2009 Jan]安全路经Travel【最短路树】【树链剖分】【线段树】

355人阅读 评论(0) 收藏 举报
分类:

【题目链接】

【hzwer的题解】orz

倍增求lca,根节点的深度不能从0开始。

线段树手滑打跪了orz,WA1发。

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

using namespace std;

typedef pair<int, int> pii;

const int maxn = 100005, maxm = 200005, maxk = 18, inf = 0x3f3f3f3f;

int n, m, head[maxn], cnt, pre[maxk][maxn], dis[maxn], depth[maxn], size[maxn], son[maxn], id[maxn], clo, top[maxn], tr[maxn << 2], tagv[maxn << 2];

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

struct _data {
	int u, v, w;
} e[maxm];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

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

priority_queue<pii, vector<pii>, greater<pii> > q;

inline void dijkstra() {
	for(int i = 1; i <= n; i++) dis[i] = inf;
	dis[1] = 0;
	q.push(pii(0, 1));
	while(!q.empty()) {
		pii t = q.top(); q.pop();
		int u = t.second;
		if(t.first > dis[u]) continue;
		for(int i = head[u]; ~i; i = g[i].next) if(dis[g[i].v] > dis[u] + g[i].w) {
			dis[g[i].v] = dis[u] + g[i].w;
			pre[0][g[i].v] = u;
			q.push(pii(dis[g[i].v], g[i].v));
		}
	}
}

inline void dfs1(int x) {
	size[x] = 1;
	for(int i = head[x]; ~i; i = g[i].next) {
		depth[g[i].v] = depth[x] + 1;
		dfs1(g[i].v);
		size[x] += size[g[i].v];
		if(size[g[i].v] > size[son[x]]) son[x] = g[i].v;
	}
}

inline void dfs2(int x, int tp) {
	top[x] = tp; id[x] = ++clo;
	if(son[x]) dfs2(son[x], tp);
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ son[x])
		dfs2(g[i].v, g[i].v);
}

inline int getlca(int u, int v) {
	if(depth[u] < depth[v]) swap(u, v);
	for(int i = maxk - 1; i >= 0; i--) if(depth[pre[i][u]] >= depth[v]) u = pre[i][u];
	for(int i = maxk - 1; i >= 0; i--) if(pre[i][u] != pre[i][v]) u = pre[i][u], v = pre[i][v];
	return u == v ? u : pre[0][u];
}

inline void pushup(int p) {
	tr[p] = min(tr[p << 1], tr[p << 1 | 1]);
}

inline void pushdown(int p) {
	if(tagv[p] != inf) {
		tagv[p << 1] = min(tagv[p << 1], tagv[p]);
		tagv[p << 1 | 1] = min(tagv[p << 1 | 1], tagv[p]);
		tr[p << 1] = min(tr[p << 1], tagv[p]);
		tr[p << 1 | 1] = min(tr[p << 1 | 1], tagv[p]);
		tagv[p] = inf;
	}
}

inline void modify(int p, int l, int r, int x, int y, int w) {
	if(x <= l && r <= y) {
		tr[p] = min(tr[p], w);
		tagv[p] = min(tagv[p], w);
		return;
	}
	int mid = l + r >> 1;
	pushdown(p);
	if(x <= mid) modify(p << 1, l, mid, x, y, w);
	if(y > mid) modify(p << 1 | 1, mid + 1, r, x, y, w);
	pushup(p);
}

inline int query(int p, int l, int r, int x) {
	if(l == r && r == x) return tr[p];
	int mid = l + r >> 1;
	pushdown(p);
	if(x <= mid) return query(p << 1, l, mid, x);
	if(x > mid) return query(p << 1 | 1, mid + 1, r, x);
}

inline void modifychain(int lca, int u, int w) {
	for(; top[lca] != top[u]; u = pre[0][top[u]])
		modify(1, 1, clo, id[top[u]], id[u], w);
	if(lca != u) modify(1, 1, clo, id[son[lca]], id[u], w);
}

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
	for(int i = 1; i <= m; i++) {
		int u = iread(), v = iread(), w = iread();
		add(u, v, w); add(v, u, w);
		e[i] = (_data){u, v, w};
	}
	dijkstra();

	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
	for(int i = 2; i <= n; i++) add(pre[0][i], i, 0);

	depth[1] = 1;
	dfs1(1); dfs2(1, 1);
	for(int j = 1; j < maxk; j++) for(int i = 1; i <= n; i++)
		pre[j][i] = pre[j - 1][pre[j - 1][i]];
	memset(tr, 0x3f, sizeof(tr));
	memset(tagv, 0x3f, sizeof(tagv));

	for(int i = 1; i <= m; i++) {
		int u = e[i].u, v = e[i].v, w = e[i].w;
		if(pre[0][u] != v && pre[0][v] != u) {
			int lca = getlca(u, v);
			modifychain(lca, v, dis[u] + w + dis[v]);
			modifychain(lca, u, dis[v] + w + dis[u]);
		}
	}

	for(int i = 2; i <= n; i++) {
		int res = query(1, 1, clo, id[i]);
		printf(res == inf ? "-1\n" : "%d\n", res - dis[i]);
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:107827次
    • 积分:4828
    • 等级:
    • 排名:第6167名
    • 原创:392篇
    • 转载:0篇
    • 译文:0篇
    • 评论:48条
    膜拜神犇
    最新评论