2017-5-14 湘潭市赛 Highway 先获得直径S,T。则一开始S,T相连,然后其他的点如果离S更远那么连在S,否则T;
Highway
Accepted : Submit :
Time Limit : MS Memory Limit : KB Highway In ICPCCamp there were n towns conveniently numbered with ,,…,n connected with (n−) roads. The i-th road connecting towns ai and bi has length ci. It is guaranteed that any two cities reach each other using only roads. Bobo would like to build (n−) highways so that any two towns reach each using only highways. Building a highway between towns x and y costs him δ(x,y) cents, where δ(x,y) is the length of the shortest path between towns x and y using roads. As Bobo is rich, he would like to find the most expensive way to build the (n−) highways.
Input The input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains an integer n. The i-th of the following (n−) lines contains three integers ai, bi and ci. ≤n≤
≤ai,bi≤n
≤ci≤
The number of test cases does not exceed . Output For each test case, output an integer which denotes the result.
Sample Input Sample Output Source
XTU OnlineJudge /**
题目:Highway
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267
题意:给定n个点,以及n-1条边,就是一颗无根树。两点之间的距离为他们的最短距离。现在要利用n个点之间的各自的最短距离,把他们转化为另一颗无根树,使得所有点有n-1条边相连,且边的长度和最大。
即:新的无根树,两个点之间的距离为原来无根树他们的最短距离。如何构造无根树,才能使边的总长度最大。求出这个长度值。
思路:
先获得直径S,T。则一开始S,T相连,然后其他的点如果离S更远那么连在S,否则T; */ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
vector<P> G[maxn];
LL dis[maxn], disS[maxn], disT[maxn];
void dfs(int u,int &x,int f)
{
if(dis[u]>dis[x]) x = u;
int len = G[u].size();
for(int i = ; i < len; i++){
int v = G[u][i].first, w = G[u][i].second;
if(v==f) continue;
dis[v] = dis[u]+w;
dfs(v,x,u);
}
}
void dfs1(int u,int f)
{
int len = G[u].size();
for(int i = ; i < len; i++){
int v = G[u][i].first, w = G[u][i].second;
if(v==f) continue;
disS[v] = disS[u]+w;
dfs1(v,u);
}
}
void dfs2(int u,int f)
{
int len = G[u].size();
for(int i = ; i < len; i++){
int v = G[u][i].first, w = G[u][i].second;
if(v==f) continue;
disT[v] = disT[u]+w;
dfs2(v,u);
}
}
int main()
{
int n;
while(scanf("%d",&n)==)
{
int u, v, w;
for(int i = ; i <= n; i++) G[i].clear();
for(int i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(P(v,w));
G[v].push_back(P(u,w));
}
int S = , T;
dis[] = ;
dfs(,S,);
dis[S] = ;
T = S;
dfs(S,T,S);
memset(disS, , sizeof disS);
dfs1(S,S);
memset(disT, , sizeof disT);
dfs2(T,T);
LL ans = disS[T];
for(int i = ; i <= n; i++){
if(i!=S&&i!=T) ans += max(disS[i],disT[i]);
}
cout<<ans<<endl;
}
return ;
}
2017-5-14 湘潭市赛 Highway 先获得直径S,T。则一开始S,T相连,然后其他的点如果离S更远那么连在S,否则T;的更多相关文章
- Gitlab一键端的安装汉化及问题解决(2017/12/14目前版本为10.2.4)
Gitlab的安装汉化及问题解决 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样... ... 装完之后感觉太简单,加了汉化补丁 ...
- hdu6212[区间dp] 2017青岛ACM-ICPC网络赛
原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...
- XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]
这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...
- 2017.07.14【NOIP提高组】模拟赛B组
Summary 这次比赛因为迟到了,少了很多时间,也受到了相应的惩罚,这是好的,是个标记牌,警醒着我.这次比赛的题目很难,也就是说,大家的得分都很低,总的来说,收获还是很大的,因为有非常多的技巧被掌握 ...
- 2017 ACM-ICPC西安网赛B-Coin
B-Coin Bob has a not even coin, every time he tosses the coin, the probability that the coin's front ...
- Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...
- 2017 icpc 沈阳网络赛
cable cable cable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2017.10.14 Java的流程控制语句switch&&随机点名器
今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作 ...
随机推荐
- golang的reflect
引用自 http://www.jb51.net/article/115002.htm 和 C 数据结构一样,Go 对象头部并没有类型指针,通过其自身是无法在运行期获知任何类型相关信息的.反射操作所需要 ...
- 个人python学习路线记录
一.入门视频 零基础入门学习Python --小甲鱼 二.博客园 python快速教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778. ...
- 用fastjson反序列化 带泛型的类
ServiceResult<User> serviceResult = JSON.parseObject(obtainResult, new TypeReference<Servic ...
- 智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎
智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎 LISP路由器
- 《VC++就业培训宝典之MFC视频教程》学习笔记
开发环境:Win7 64位 + VS2013 第三章第二节视频 在Win32工程中学习MFC开发,编译问题: Building MFC application with /MD[d] (CRT dll ...
- SpringBoot拦截器中service或者redis注入为空的问题
原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...
- Bootstrap幻灯片
Bootstrap幻灯片的制作利用到了Carousel插件,包含:左右箭头.图片.点点导航 <div id="carousel-example-generic" class= ...
- Eclipse新建java类的时候,自动创建注释
为形成个人的java代码风格,我们在项目组中进行开发的时候,可以对自己的代码进行一些格式上面的设置,具体如下: 方法一:Eclipse中设置在创建新类时自动生成注释 windows–>prefe ...
- [Linux] Ubuntu下非常给力的下载工具
转载:http://blog.csdn.net/luojiming1990/article/details/9078447 Windows下的下载工具--迅雷,之所以下载速度快,乃是它能搜索资源.为己 ...
- 删除cnpm
cnpm是npm的中国镜像:这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步. 安装:npm install -g cnp ...