POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

题目链接:http://poj.org/problem?id=2631

Description

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. 
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.

The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.

Input

Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

Output

You are to output a single integer: the road distance between the two most remote villages in the area.
 
题目大意:给你一棵树,求树上最远的两点距离为多少。(题目描述比较奇葩,你可以认为点数等于边数+1,所有运算不会超过2^31-1)
思路:从任意一点开始,找到离这个点最远的点x。再从x开始做单源最短路,离x最远的点和x就是这棵树的最远点对。(当然基于树的分治也是可以做的)
证明(参考DISCUSS):
设最长链是MN->已知[1]
设由A开始DFS得到最长路为AB->已知[2]
结论[1] MN与AB有公共点.否则MN<AM+AN<=AM+AB=BM 与已知[1]矛盾
结论[2] B是最长链的一个端点.否则由结论[1] 设K是AB上距B最近且在MN上的点 则MN=MK+KN=MK+AN-AK<=MK+AB-AK=MK+BK=BM 当取等号时MB与MN等长 符合结论[2] 否则与已知[1]矛盾 [这里假定了A不在NK上.若A在NK上 只须将上面式子中MN交换位置即可 不影响结论]
结论[3] 从B开始DFS得到的最长路径是一条最长链.由结论[2].B是最长链的一端
至此证毕

代码(0MS):

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXE = ; int dis[MAXN], head[MAXN];
int to[MAXE], next[MAXE], cost[MAXE];
int n, ecnt; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} int bfs(int st) {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(st);
dis[st] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(dis[v] > dis[u] + cost[p]) {
dis[v] = dis[u] + cost[p];
que.push(v);
}
}
}
int ed = st;
for(int i = ; i <= n; ++i)
if(dis[i] > dis[ed]) ed = i;
return ed;
} int main() {
init();
n = ;
int u, v, c;
while(scanf("%d%d%d", &u, &v, &c) != EOF) {
add_edge(u, v, c);
++n;
}
u = bfs();
v = bfs(u);
printf("%d\n", dis[v]);
}

————————————————————————————————时间的分割线————————————————————————————————————————————————

后记(2014-7-26):

当然这题还可以树DP,之前觉得麻烦用了上面的解法。现在有一个比较高大上的写法,边权是负数的时候也适用(据说上面的做法边权有负数就没用了,我懒得验证了有兴趣去验证一下吧……)。

思路很简单,树的直径必然是树上某一个点开始往下的最长链和次长链之和。而实现的时候只要保留最长链的大小就可以了,比较简单不讲了看代码就好。

代码(16MS):

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int MAXV = ;
const int MAXE = ; int head[MAXV];
int to[MAXE], next[MAXE], cost[MAXE];
int n, ecnt; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} int dfs(int u, int f, int &ans) {
int maxdep = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
int tmp = dfs(v, u, ans) + cost[p];
ans = max(ans, maxdep + tmp);
maxdep = max(tmp, maxdep);
}
return maxdep;
} int main() {
init();
n = ;
int u, v, c;
while(scanf("%d%d%d", &u, &v, &c) != EOF) {
add_edge(u, v, c);
++n;
}
int ans = ;
dfs(, , ans);
printf("%d\n", ans);
}
上一篇:安卓_构建首个应用


下一篇:C++学习笔记17,构造函数体内初始化数据成员与构造函数初始化器的区别(一)