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. 在xcode6中使用矢量图(iPhone6置配UI)

    转载出处:http://blog.xoneday.com ios应用程序是一个图像主导的产品.在开发一个应用程序时,你需要各种尺寸的图标,你需要为每个图像文件制作一个@1x尺寸和一个@2x尺寸.这样你 ...

  2. 图解http读书笔记

    以前对HTTP协议一知半解,一直不清楚前端需要对于HTTP了解到什么程度,知道接触的东西多了,对于性能优化.服务端的配合和学习中也渐渐了解到了HTTP基础的重要性,看了一些大神对HTTP书籍的推荐,也 ...

  3. Layui 使用问题汇总

    1. Layui数据表格中checkbox位置不居中 使用方法渲染的方式生成数据表格,添加了checkbox,但发现checkbox位置不居中,如下图所示 解决办法 通过layui官方社区,找到如下代 ...

  4. WebLogic MBean Monitor

    weblogic server提供了一个dashboard让我们对mbean进行图形化的展现和分析,地址是 http://localhost:7001/console/dashboard 但有时候总是 ...

  5. Android--使用XMLPull解析xml

    在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...

  6. Windows 2003 R2

    微软发布Windows Server 2003 R2版的目的是希望透过它填补Windows Server 2003 SP1和Longhorn Server之间的产品发布时间间隔. 微软向产品测试人员表 ...

  7. 自建Saltstack的repo软件源仓库

    因为Saltstack自己的repo源是在国外,在国内服务器yum安装Saltstack的时候下载软件包就非常慢,很多情况下还经常下载失败,其实软件包总大小只有10M左右,如果这样安装多台minion ...

  8. 顶点缓存对象(VBO)【转】

    http://www.cnblogs.com/hefee/p/3824300.html 顶点缓存对象(VBO) 创建VBO 绘制VBO 更新VBO 实例 GL_ARB_vertex_buffer_ob ...

  9. HDU2491 Priest John&#39;s Busiest Day

    题目链接 题意: 有n个人要进行乒乓球比赛,每一个人都一个能力值.每一个人出现的次序就是他们住的位置 如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这样的比赛一 ...

  10. C# 解决窗体假死的状态

    异步调用是CLR为开发者提供的一种重要的编程手段,它也是构建高性能.可伸缩应用程序的关键.在多核CPU越来越普及的今天,异步编程允许使用非常少的线程执行很多操作.我们通常使用异步完成许多计算型.IO型 ...