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只能针对某个表达式的值作 ...
随机推荐
- 调用上一个页面的js方法
点击商品分类,弹出下框 点击确定,将选中的类别的name和唯一的code返回到上个页面 function save(){ var ids = getIdSelections(); jp.get(&qu ...
- JNI之数组
Array Operations -- 数组操作 1.GetArrayLength jsize GetArrayLength(JNIEnv *env, jarray array); Returns t ...
- JAVA EE 学习笔记
http://www.cnblogs.com/kuangdaoyizhimei/category/701794.html http://www.cnblogs.com/liunanjava/p/445 ...
- XAMPP安装与多虚拟目录地址设置
前端开发集成环境-XAMPP 在前端开发中,经常需要进行请求的调试等都需要一个服务器环境,这时类似wamp.XAMPP就是我们最后的选择,集成apache.php.mysql等一应俱全,不需要去单独配 ...
- pdf.js 添加自定义loading动画
最近做了个手机端pdf预览的功能,用到pdf.js这个库,效果还不错.但是在网络差.文件大时,页面一直空白,体验不是很好. 于是加了个loading动画. <div id="loadi ...
- 删除cnpm
cnpm是npm的中国镜像:这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步. 安装:npm install -g cnp ...
- DEDECMS之0day入侵总结
1.查看dedecms最后升级版本:http://xxx.com/data/admin/ver.txt 2.利用网上公开之0day进行对应版本之入侵 ps:dedecms默认CMS后台:http:// ...
- RenderMonkey基本使用方法【转】
RenderMonkey基本使用方法 楔子: 差不多从年中开始由于工作需要,开始研究Direct3D,这是继大二开始自学DX开始,睽违了6年后再重新学习DX.虽然时间很久了,但是幸亏还是有点基础,所以 ...
- javascript 正则表达式判断只能是中文、英文或者中文加英文
var reglx =/^[\u4e00-\u9fa5a-zA-Z]+$/ 这个是至少有一个中文或者英文 var reglx =/^[\u4e00-\u9fa5a-zA-Z]*$/ 这个是0个以上的中 ...
- win下写任务提交给集群
一,复制和删除hdfs中的文件 import org.apache.hadoop.fs.{FileSystem, Path} import org.apache.spark.{SparkConf, S ...