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;的更多相关文章

  1. Gitlab一键端的安装汉化及问题解决(2017/12/14目前版本为10.2.4)

    Gitlab的安装汉化及问题解决 一.前言 Gitlab需要安装的包太TM多了,源码安装能愁死个人,一直出错,后来发现几行命令就装的真是遇到的新大陆一样... ... 装完之后感觉太简单,加了汉化补丁 ...

  2. hdu6212[区间dp] 2017青岛ACM-ICPC网络赛

    原题: BZOJ1032 (原题数据有问题) /*hdu6212[区间dp] 2017青岛ACM-ICPC网络赛*/ #include <bits/stdc++.h> using name ...

  3. XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]

    这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...

  4. 2017.07.14【NOIP提高组】模拟赛B组

    Summary 这次比赛因为迟到了,少了很多时间,也受到了相应的惩罚,这是好的,是个标记牌,警醒着我.这次比赛的题目很难,也就是说,大家的得分都很低,总的来说,收获还是很大的,因为有非常多的技巧被掌握 ...

  5. 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 ...

  6. 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 ...

  7. 2017 icpc 南宁网络赛

    2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...

  8. 2017 icpc 沈阳网络赛

    cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. 2017.10.14 Java的流程控制语句switch&&随机点名器

    今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构     * A:switch语句解构       * a:switch只能针对某个表达式的值作 ...

随机推荐

  1. IO流--字符流缓冲技术

    缓冲技术是为了提高数据的读写效率而提出的. (1)字符流的缓冲读 在字符流的缓冲技术中提供了一个newLine()方法,这个方法是跨平台的 在读数据的时候采用读完直接刷新的方式可以保证断电后数据不会丢 ...

  2. ListView控件(上)数据适配器:ListView绑定监听是SetOnItemClickListener

    (一) 1.效果图: 2.MainActivity.java package com.example.app5; import android.support.v7.app.AppCompatActi ...

  3. 百度地图API的事件处理:覆盖物的如何阻止冒泡

    百度地图,为了让事件使用的更方便,进行一层封装 详情可以看官方的文档 http://developer.baidu.com/map/jsdevelop-5.htm 主要的修改点: 1. 使用事件代理. ...

  4. HTTPS.SYS怎样使用HTTPS

    HTTPS.SYS怎样使用HTTPS 参考了MORMOT的官方文档:http://blog.synopse.info/post/2013/09/04/HTTPS-communication-in-mO ...

  5. 【tomcat】FileNotFoundException: C:\Program Files\Java\apache-tomcat-8.5.11-geneshop3\webapps\ROOT\index.html (拒绝访问。)

    新装系统后,tomcat启动起来 提示如下错误: Caused by: java.io.FileNotFoundException: C:\Program Files\Java\apache-tomc ...

  6. WebApi_HelpPage

        HelpPage是描述WebApi接口信息的Web页,根据项目编译的Xml文件生成,包括接口的Router地址.请求参数.请求参数示例.响应参数.响应参数示例等,可方便调用方快速了解接口信息 ...

  7. javascript快速入门19--定位

    元素尺寸 获取元素尺寸可以使用下面几种方式 元素的style属性width,height,但这些属性往往返回空值,因为它们只能返回使用行内style属性定义在元素上的样式 元素的currentStyl ...

  8. centos 6.5 python2.6.6 zbar 安装

       经过数次折腾,终于搞明白了这个zbar的安装顺序.   1.先安装http://zbar.sourceforge.net/download.html 下的zbar,   2.python 安装z ...

  9. Oracle数据类型,函数与存储过程

    字符串类型    固定长度:char nchar    n 表示Unicode编码    可变长度: varchar2 nvarchar2 数字类型:number(P,S)P:整数位数,S小数位数   ...

  10. Android短信发送器(2)

    在上一篇的<Android短信发送器>当中.发送功能并不完好.当发送内容超过限定字数时,短信就会发送失败,此时就须要推断内容是否超过限制,假设不超过限制.就直接发送,反之.则对其进行处理再 ...