UvaLive 5026 Building Roads
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 ∈ U}, min{d[u] : u ∈ L, d[v] : 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的更多相关文章
- poj 3625 Building Roads
题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- Building roads
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- 洛谷——P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
随机推荐
- C语言提供的位运算符
运算符 含义 描述 & 按位与 如果两个相应的二进制位都为1,则该位的结果值为1,否则为0 | 按位或 两个相应的二进制位中只要有一个为1,该位的结果值为1 ^ 按位异或 若参加运算的两个 ...
- Linux 网络编程八(epoll应用--大并发处理)
//头文件 pub.h #ifndef _vsucess #define _vsucess #ifdef __cplusplus extern "C" { #endif //服务器 ...
- jacob下载问题, Office word 此文件正由另一应用程序或用户使用的解决方法
http://jingyan.baidu.com/article/75ab0bcbd6682fd6864db2db.html
- Qt——正则表达式
在项目中经常会遇到对字符串进行操作的情况,我们可以直接使用QString的一些函数,但QT提供了一个更加强大的类——QRegExp,使用正则表达式来操作字符串. 先说说我最近遇到的几个问题: 1.对输 ...
- memcached工作原理与优化建议
申明,本文为转载文:http://my.oschina.net/liuxd/blog/63129 工作原理 基本概念:slab,page,chunk. slab,是一个逻辑概念.它是在启动memcac ...
- java 十六进制颜色对照表
我们在编程中常常用到十六进制颜色码. 下面是颜色码对照表-英文名称-十六进制-RGB: 英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅 ...
- ASP.NET MVC 5 入门教程 (1) 新建项目
文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html 下一节:ASP.NET ...
- 破解windows server 2008 的登录密码。有效的
今天拿到一块以前服务器上替换下来的老盘,里面还有系统.挂载到另外一台闲置服务器,发现密码忘记了, 结果拿出pe和以前修改xp和2003的系统那样去修改发现不行,不知道为什么,修改SAM文件明明提示成功 ...
- JavaScript里面三个等号和两个等号有什么区别?
1.对于string,number等基础类型,==和===是有区别的 a)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 b)同类型比较,直接进 ...
- 关于php析构函数的一个有趣问题
随着面向对象编程的普遍展开,面向对象展现了其中很多有趣的问题.相信很多初学者学习php面向对象时会接触两个函数,构造函数与析构函数.构造函数似乎用的更多,析构函数用的较少(相对初学者有限编程经验而言, ...