题目链接: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);
}

POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)的更多相关文章

  1. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  2. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  3. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  4. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  5. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

  6. POJ 2631 Roads in the North (树的直径)

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  7. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  8. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. POJ [P2631] Roads in the North

    树的直径 树的直径求法: 任取一点u,找到树上距u最远的点s 找到树上距s点最远的点t,s->t的距离即为所求 #include <iostream> #include <cs ...

随机推荐

  1. injection for Xcode10使用方法

    对于一个使用Xcode的使用者来说,麻烦的地方在于使用代码布置界面时候的调试,5s改一下代码,用10s查看修改效果,如果电脑配置稍低,时间更长,这是病,得治,哈哈.下面就来说一下injection的使 ...

  2. 【模板】缩点(tarjan,DAG上DP)

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...

  3. vue使用axios调用豆瓣API跨域问题

    最近做了一个vue小demo,使用了豆瓣开源的API,通过ajax请求时需要跨域才能使用.   封面.jpg 一.以下是豆瓣常用的开源接口: 正在热映 :https://api.douban.com/ ...

  4. angularjs中 $watch 和$on 2种监听的区别?

    1.$watch简单使用 $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEq ...

  5. 「PHP」设计模式介绍

    引言   最近再看PHP设计模式相关的一些技术文章,网上有关PHP的设计模式范例很少,这里做一些总结仅供参考,不足之处望提出. 参考资料: <大话设计模式>程杰   什么是设计模式   设 ...

  6. mysql 主主架构,多入口 互为备份

    ,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入口,增加高可用.不过多主需要考虑自增长ID ...

  7. 帝国cms伪静态设置方法(收藏)

    众所周知,动态页面不利于收录和排名.伪静态可以完美的解决这问题,配合百度云加速CDN,可以让动态页面有静态页面一样快的访问速度. 今天开拓族给大家带来帝国CMS伪静态的详细设置方法. 1.栏目设置为动 ...

  8. 『Python基础-15』递归函数 Recursion Function

    什么是递归函数 一种计算过程,如果其中每一步都要用到前一步或前几步的结果,称为递归的.用递归过程定义的函数,称为递归函数,例如连加.连乘及阶乘等.凡是递归的函数,都是可计算的,即能行的. 递归就是一个 ...

  9. (转)service apache2 restart失败

    https://askubuntu.com/questions/431925/how-to-restart-apache2-when-i-get-a-pid-conflict sudo kill -9 ...

  10. 决策树算法之ID3与C4.5的理解与实现

    github:代码实现 本文算法均使用python3实现 1. 决策树   决策树(decision tree)是一种基本的分类与回归方法(本文主要是描述分类方法),是基于树结构进行决策的,可以将其认 ...