传送门

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. c# 调用打印机

    1.本地打印机 //添加引用并using System.Management; public static void AvailablePrinters() { ManagementScope ms ...

  2. [转]Hive/Beeline 使用笔记

    FROM : http://www.7mdm.com/1407.html Hive: 利用squirrel-sql 连接hive add driver -> name&example u ...

  3. U3D physics总结

    物理系统基于collider, 没有collider的物体不会发生任何主动和被动的物理交互,也不会产生trigger相关消息. 当且仅当A和B都有碰撞体时,两者才有可能发生交互,才有可能产生trigg ...

  4. Gruntjs: task之文件映射

    由于大多数的任务执行文件操作,Grunt提供了一个强大的抽象声明说明任务应该操作哪些文件.这里总结了几种src-dest(源文件-目标文件)文件映射的方式,提供了不同程度的描述和控制操作方式. 1. ...

  5. HMAC-MD5算法原理及实现

    以下是分析节选,对于更详细的描述可以查阅RFC2104文档.     HMAC需要一个加密用散列函数(表示为H)和一个密钥K. 假设H是一个将数据块用一个基本的迭代压缩函数来加密的散列函数. 用B来表 ...

  6. Go视频教程整理

    [Go Web基础]01博客项目设计 |Go视频教程|Go语言基础 http://www.tudou.com/programs/view/gXZb9tGNsGU/ [Go Web基础]02初窥 Web ...

  7. Opencv step by step - 视频进度条

    上一个博文只是进行了视频播放,这里加上进度条. 下面是修改好的代码: #include <cv.h> #include <highgui.h> /* * tan@ubuntu: ...

  8. 1.SQLAlchemy文档-简介(中文版)

    Python的SQL工具包和对象关系映射器     SQLAlchemy的是Python的SQL工具包和对象关系映射器,让应用程序开发人员可以使用上SQL的强大功能和灵活性.     它提供了一套完整 ...

  9. ModelProxy 前端接口配置建模框架

    ModelProxy    轻量级的接口配置建模框架(1) 先看一下这个博客说明为什么需要用ModelProxy的前端轻量级的框架吧:  http://developer.51cto.com/art/ ...

  10. JS中decodeURI()与decodeURIComponent()区别

    decodeURI()定义和用法:decodeURI() 函数可对 encodeURI() 函数编码过的URI 进行解码. 语法:decodeURI(URIstring) 参数 描述:URIstrin ...