传送门

Time Limit: 3000MS

Description

There is a magic planet in the space. There is a magical country on the planet. There are N cities in the country. The country is magical because there are exactly N −1 magic roads between the N cities, and from each city, it is possible to visit any other city. But after the huge expansion of the country, the road system seems to be messy. The moderator decided to rebuild the road system. As a worker, I cannot do too much things. I could just move one road to another position connecting arbitrary 2 cities using my magic, keeping its length unchanged. Of course, afterwards all the N cities have to be still connected. I wonder how to move in order to make the farthest distance between any two cities minimum. Could you solve it for me?

Input

The first line of the input is one integer T (T ≤ 10), and then T test cases follow. Each test case begins with a line contains only one integer N (N ≤ 2500), means there are N magic cities. The cities are numbered from 0 to N − 1. Following N − 1 lines, each line has 3 integers a, b and c, means there is a magic road between a and b with distance c. (0 ≤ a, b < N, 0 < c ≤ 1000)

Output

For each test case, output the case number and the corresponding minimum farthest distance. See sample for detailed format.

Sample Input

2

4

0 1 2

1 2 2

2 3 2

5

0 1 1

1 2 2

2 3 3

3 4 4

Sample Output

Case 1: 4

Case 2: 7

-----------------------------------------------------------

这是2010年天津站的B题

题意:给定一棵带边权的树,可将其中的一条边重连,形成一棵新树,求新树中最长路的最小值。

Solution:

枚举边,求重连该边后新树中最长路径的最小值。

求法:

设删去长为c的边(u,v)后形成的两子树的点集分别是 L, R

分别求两子树中从每个节点u出发的最长距离d[u](复杂度O(n), 参考这篇博客), 显然新边应连在两子树中d最小的两节点之间。

这样得到的新树中的最长路径就是max(max{d[u], U}, min{d[u] : ∈ L, d[v] : ∈ R} + c)

总复杂度O(n^2)。

 Implementation:

#include <bits/stdc++.h>
using namespace std;
const int N(+);
typedef pair<int,int> P;
struct edge{
int u, v, c, flag, nt;
}E[N<<];
int head[N], dp[][N];
void dfs1(int u, int f){
dp[][u]=dp[][u]=;
for(int i=head[u]; ~i; i=E[i].nt){
int &v=E[i].v, &c=E[i].c;
if(v==f||E[i].flag) continue;
dfs1(v, u);
if(dp[][v]+c > dp[][u])
dp[][u]=dp[][u], dp[][u]=dp[][v]+c;
else dp[][u]=max(dp[][u], dp[][v]+c);
}
}
P dfs2(int u, int f){
int mi, ma=mi=max(dp[][u], dp[][u]);
for(int i=head[u]; ~i; i=E[i].nt){
int &v=E[i].v, &c=E[i].c;
if(v==f||E[i].flag) continue;
dp[][v]=c+max(dp[][u], dp[][u]==dp[][v]+c?dp[][u]:dp[][u]);
P res=dfs2(v, u);
mi=min(mi, res.first), ma=max(ma, res.second);
}
return {mi, ma};
}
int main(){
int T; scanf("%d", &T);
for(int n, cs=, ans; T--;){
scanf("%d", &n);
memset(head, -, sizeof(head));
for(int i=, u, v, c, id=; i<n; ++i){
scanf("%d%d%d", &u, &v, &c);
E[id]={u, v, c, , head[u]}, head[u]=id++;
E[id]={v, u, c, , head[v]}, head[v]=id++;
}
P p1, p2;
ans=INT_MAX;
for(int i=; i<(n-)<<; i+=){
E[i].flag=E[i^].flag=;
int &u=E[i].u, &v=E[i].v, &c=E[i].c;
dfs1(u, u), dp[][u]=, p1=dfs2(u, u);
dfs1(v, v), dp[][v]=; p2=dfs2(v, v);
ans=min(ans, max(max(p1.second, p2.second), p1.first+p2.first+c));
E[i].flag=E[i^].flag=;
}
printf("Case %d: %d\n", ++cs, ans);
}
}

UvaLive 5026 Building Roads的更多相关文章

  1. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  4. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  5. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  7. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  8. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

随机推荐

  1. UITextView限制字数与行数

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...

  2. Hadoop和Spark的异同

    谈到大数据,相信大家对Hadoop和Apache Spark这两个名字并不陌生.但我们往往对它们的理解只是提留在字面上,并没有对它们进行深入的思考,下面不妨跟我一块看下它们究竟有什么异同. 解决问题的 ...

  3. 【C#】窗体动画效果

    通过调用API可以实现C#窗体的动画效果,主要调用user32.dll的行数AnimateWindow 1.函数申明 [System.Runtime.InteropServices.DllImport ...

  4. GeoServer 常见问题总结

    Geoserver安装环境 Geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办? 存储位置:C:\ ...

  5. php基础33:正则匹配-perl

    <?php //1.搜索数组中的相匹配的字符串 //preg_grep() 返回一个数组 $language = array("php","asp",&q ...

  6. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  7. Qt——右键菜单

    所谓“右键菜单”,我们可以这样来看:右键+菜单.所以我们可以定义一个菜单,然后重写鼠标点击事件,令菜单在鼠标右击的时候弹出来.这种方法是可以的,但是Qt提供了一种专门用于右键菜单的方法,且看下面这个属 ...

  8. JS 之DOM range对象

    DOM范围 DOM中的range对象是DOM2中新定义的接口.通过这个对象可以选择文档中的某个区域,而不必考虑节点的界限. 创建范围 document.createRange()创建一个范围,这个范围 ...

  9. Ubuntu下类似于Total Commander的两个工具

    Total Commander for linux Is there a Linux version? Unfortunately not. Because of problems with port ...

  10. Android权限列表permission说明

    网络上不乏android权限列表,但是很少有将列表和使用方法放在一起的,所以特此总结一下 需要在AndroidManifest.xml中定义相应的权限(以获取internet访问权限为例),如下: & ...