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 ...
随机推荐
- Android优化——UI优化(三)使用ViewStub延迟加载
使用ViewStub延迟加载 1.ViewStub延迟加载 ViewStub是一个不可见的,大小为0的View,最佳用途就是实现View的延迟加载,在需要的时候再加载View,可Java中常见的性能优 ...
- 什么是json
http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html http://edu.51cto.com/lesson/id-71123 ...
- Linux Shell编程一
交互模式 --当Shell收到用户输入命令后,就开始执行这项命令,并把结果显示到屏幕上,结束后Shell又会显示系统提示符,等待用户输入下一条命令. 后台运行 --后台运行的符号为"& ...
- 深入了解view以及自定义控件
参考文章: http://blog.csdn.net/guolin_blog/article/details/12921889 Android LayoutInflater原理分析,带你一步步深入了解 ...
- &10 基本数据结构——栈,队列和链表
#1,栈(stack) 定义[来自百度]:栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素 ...
- Android调用基于.net的WebService
在实际开发项目中,有时候会为Android开发团队提供一些接口,一般是以asmx文件的方式来承载.而公布出去的数据一般上都是标准的json数据.但是在实际过程中,发现Android团队那边并不是通过将 ...
- Androd Studio layout页面布局无法预览
Could not initialize class android.support.v7.internal.widget.ActionBarOverlayLayout 导致无法看到布局页面,解决方法 ...
- 关于matlab中特殊字符, 上标和下标
'T=25\circC',(摄氏度) 下标用 _{下划线} 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...
- Windows10+Ubuntu双系统安装[多图]
最近因为毕设重新回归Ubuntu,手头有一台装了Win10的ThinkPad X240s,最终成功完成了Windows 10 教育版和Ubuntu Kylin 15.10 的双系统配置,下文(多图慎入 ...
- JAVA中获取当前系统时间
一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...