Highway

Accepted : 122   Submit : 393
Time Limit : 4000 MS   Memory Limit : 65536 KB

Highway

In ICPCCamp there were n towns conveniently numbered with 1,2,…,n connected with (n−1) 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−1) 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−1) 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−1) lines contains three integers ai , bi and ci .

  • 1≤n≤105
  • 1≤ai,bi≤n
  • 1≤ci≤108
  • The number of test cases does not exceed 10 .

Output

For each test case, output an integer which denotes the result.

Sample Input

5
1 2 2
1 3 1
2 4 2
3 5 1
5
1 2 2
1 4 1
3 4 1
4 5 2

Sample Output

19
15

Source

XTU OnlineJudge

题意:要把所有的边都联通,并要求权值之和最大

解法:因为是颗树,那就没必要讨论两点的最短路了(毕竟树是没有回路的),那么重点是落在了如何找到权值之和最大的方法
我们找到这颗树相距最远的两个点(树的直径)A,B 剩余的点取距离A或者B最远的那条,需要进行两次dfs,距离保存最大的
然后加起来就行,因为A到B我们多加了一次,再减去就是结果
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int inf=(1<<30);
 5 const int maxn=100005;
 6 ll pos;
 7 ll n,ans,vis[maxn],in[maxn];
 8 vector<pair<int,int>>e[maxn];
 9 ll sum;
10 void dfs(int v,ll cnt)
11 {
12     if(ans<cnt)
13     {
14         ans=cnt;
15         pos=v;
16     }
17     if(vis[v])return;
18     vis[v]=1;
19     for(int i=0; i<e[v].size(); i++)
20         //    cout<<e[v][i].first;
21         if(!vis[e[v][i].first])
22             dfs(e[v][i].first,cnt+e[v][i].second);
23 }
24 ll dis1[123456],dis2[123456];
25 void DFS(int v,ll cnt,ll dis[])
26 {
27     if(vis[v]) return;
28     vis[v]=1;
29     dis[v]=cnt;
30     for(int i=0; i<e[v].size(); i++)
31         //    cout<<e[v][i].first;
32         if(!vis[e[v][i].first])
33             DFS(e[v][i].first,cnt+e[v][i].second,dis);
34 }
35 int main()
36 {
37     int n,m;
38     ans=0;
39     while(~scanf("%d",&n))
40     {
41         ans=0;
42         memset(dis1,0,sizeof(dis1));
43         memset(dis2,0,sizeof(dis2));
44         memset(in,0,sizeof(in));
45         memset(vis,0,sizeof(vis));
46         for(int i=0;i<=n;i++)
47         {
48             e[i].clear();
49         }
50         for(int i=1; i<n; i++)
51         {
52             ll u,v,w;
53             scanf("%d%d%d",&u,&v,&w);
54             e[u].push_back({v,w});
55             e[v].push_back({u,w});
56         }
57         dfs(1,0);
58         ll cnt=ans;
59         ans=0;
60         memset(vis,0,sizeof(vis));
61         ans=0;
62         DFS(pos,0,dis1);
63         memset(vis,0,sizeof(vis));
64         ans=0;
65         dfs(pos,0);
66
67         memset(vis,0,sizeof(vis));
68         DFS(pos,0,dis2);
69         memset(vis,0,sizeof(vis));
70         ll cot=ans;
71         //cout<<cot<<" "<<cnt<<endl;
72         ll Max=max(cnt,cot);
73         //cout<<Max<<endl;
74         sum=0;
75         for(int i=1;i<=n;i++)
76         {
77             sum+=max((ll)dis1[i],(ll)dis2[i]);
78         }
79         printf("%lld\n",sum-Max);
80     }
81     return 0;
82 }

转载于:https://www.cnblogs.com/yinghualuowu/p/6875605.html

2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Highway相关推荐

  1. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Partial Sum

    Partial Sum Accepted : 124   Submit : 450 Time Limit : 3000 MS   Memory Limit : 65536 KB  Partial Su ...

  2. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Strange Optimization

    Strange Optimization Accepted : 89   Submit : 350 Time Limit : 1000 MS   Memory Limit : 65536 KB  St ...

  3. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛:H—Highway

    题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267 Highway In ICPCCamp there wer ...

  4. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛:I— Strange Optimization

    题目链接:传送门 Strange Optimization Bobo is facing a strange optimization problem. Given  n,m , he is goin ...

  5. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛:E—Partial Sum

    题目链接:传送门 Partial Sum Bobo has a integer sequence  a1,a2,-,an  of length  n . Each time, he selects t ...

  6. XTU 1264 Partial Sum 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)

    Partial Sum   Accepted : 87   Submit : 366 Time Limit : 3000 MS   Memory Limit : 65536 KB Partial Su ...

  7. xtu 1268 Strange Optimization 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)

    Strange Optimization Bobo is facing a strange optimization problem. Given  n,m , he is going to find ...

  8. XTU 1268 Strange Optimization 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)

    Strange Optimization   Accepted : 65   Submit : 286 Time Limit : 1000 MS   Memory Limit : 65536 KB S ...

  9. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南) 暨 第九届湘潭市大学生程序设计比赛H.Highway(树的直径)

    Highway Accepted : 122   Submit : 393 Time Limit : 4000 MS   Memory Limit : 65536 KB Highway In ICPC ...

最新文章

  1. Java的几种常见接口用法
  2. 网络营销——网络营销专员如何做好网站外链优化工作?
  3. 数组 ——求出一组数的最大值(用数组实现)
  4. ui kit html,开始使用
  5. OutputStreamWriter介绍代码实现
  6. Bash脚本教程之函数
  7. Spring @Value取值为null或@Autowired注入失败
  8. online游戏服务器架构--数据库及事件相关 .
  9. 渗透测试web未设置http头 Strict Transport Security
  10. 【报告分享】女性自我保护手册,教你应对10种常见危险处境.pdf(附189页pdf下载链接)...
  11. Scikit-learn:主要模块和基本使用方法
  12. Ubuntu Linux 8.04 Vsftp 假造用户设置
  13. 阿里云服务器跟淘宝上租的服务器有什么区别?
  14. Halcon 3D 计算3D模型的3D表面法线
  15. 第五课 大数据技术之Fink1.13的实战学习-状态编程和容错机制
  16. 网络游戏外挂制作(3)
  17. 5g消息与服务器,5G消息app下架 中国移动回应会重新上架
  18. RealView Development Suite 4.0 Professional安装
  19. MemSQL 的安装和简单使用 比Mysql快30倍的关系型数据库
  20. Java软件工程师[初级测试题]

热门文章

  1. 微信公众号如何实现在线报修系统?
  2. 百万 QPS 前端性能监控系统设计与实现
  3. smb服务介绍及配置详解
  4. Cas9系统新应用—构建DNA时钟记录生物学事件的时间信息
  5. 我的天哪, 什么是SOA架构
  6. 为什么很多后端写接口都不按照 restful 规范?
  7. 手机端,电脑端,MQTT服务端 连接测试
  8. YOLOv5进行训练时出现AssertionError: Image Not Found +路径的问题
  9. .py和.ipynb的小知识
  10. 2019知识付费:吃瓜看戏到知乎,学习物理上B站