题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267

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

题目大意:给你一颗已知的树,求出新建的一颗权值最大的树的权值

解题思路:树形dp,求出每个点到树其他点的最长距离再减去树中的最长路径。

将每个点的最长路径添加到新建的树中,除了树中最长的那条路径会重复一次(构成一个环),不会形成其他环,而所有的点都添加到了新建的树中,于是构成了一颗权值最大的树。

dp[u][0]:u到子树节点的最长距离,dp[u][1]:u到子树节点外其他节点的最长距离

dp[u][0]很容易在dfs中计算出来。

dp[v][1]:若v不是u子树最长路径经过的点,dp[v][1] = max( dp[u][1]+Luv , dp[u][0]+Luv )

若v是u子树最长路径所经过的点,dp[v][1] = max( dp[u][1]+Luv , dp[u][0]+Se[u])

Se[u]带表u的子树中第二长的路径长度。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <algorithm>
#include <map>
using namespace std;
typedef  __int64 ll;
const int N = 100100;
const int M = 20;
const int INF = 0x3fffffff;struct  Edge
{int node,len;Edge*next;
}m_edge[N*2];
int Ecnt;
Edge*head[N];
int vis[N],flag[N];
ll dp[N][2],second[N];
//dp[u][0]:u到子树节点的最长距离,dp[u][1]:u到子树节点外其他节点的最长距离
//second[u]:子树节点到u的第二长路径,flag[v]:节点v的上层节点的最长路径是否经过vvoid init()
{Ecnt = 0;fill( head , head+N , (Edge*)0 );fill( flag , flag+N , 0 );fill( second , second+N , 0 );
}void mkEdge( int a , int b , int c )
{m_edge[Ecnt].node = a;m_edge[Ecnt].len = c;m_edge[Ecnt].next = head[b];head[b] = m_edge+Ecnt++;m_edge[Ecnt].node = b;m_edge[Ecnt].len = c;m_edge[Ecnt].next = head[a];head[a] = m_edge+Ecnt++;
}ll dfs1( int u )
{vis[u] = true; dp[u][0] = 0;for( Edge*p = head[u] ; p ; p = p->next ){int v = p->node;if( !vis[v] ){ll w = dfs1( v );dp[u][0] = max( dp[u][0] , w+p->len );}}return dp[u][0];
}void dfs2( int u  )
{vis[u] = true;int fg = 0;for( Edge*p = head[u] ; p ; p = p ->next ){int v = p->node;if( !vis[v] ){if( !fg && dp[u][0] == dp[v][0]+p->len ) { flag[v] = 1;fg = 1; }else second[u] = max( second[u] , dp[v][0]+p->len );dfs2( v );}}
}void dfs3( int u )
{vis[u] = true;for( Edge*p = head[u] ; p ; p = p->next ){int v = p->node;if( !vis[v] ){if( !flag[v] ) dp[v][1] = max( dp[u][1]+p->len , dp[u][0]+p->len );else dp[v][1] = max( dp[u][1]+p->len , second[u]+p->len );dfs3( v );}}
}int main()
{int n,a,b,c;while( ~scanf("%d",&n) ){init();for( int i = 0 ; i < n-1 ; ++i ){scanf("%d%d%d",&a,&b,&c);mkEdge( a , b , c );}memset( vis , 0 , sizeof(vis) );dfs1( 1 );memset( vis , 0 , sizeof(vis) );dfs2( 1 );memset( vis , 0 , sizeof(vis) );dfs3( 1 );ll rec = 0,ans = 0;for( int i = 1 ; i <= n ; ++i ){ll t = max( dp[i][0] , dp[i][1] );ans += t;rec = max( rec , t );}ans -= rec;printf("%I64d\n",ans);}return 0;
}

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

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

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

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

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

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

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

  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. 「模型解读」深度学习网络只能有一个输入吗
  2. Android关于SQLiteOpenHelper的封装
  3. VC6自定义注释代码快捷键
  4. 034_jdbc-mysql-C3P0
  5. 华为鸿蒙系统发布完整视频,华为发布鸿蒙系统 华为鸿蒙系统发布会完整视频 华为鸿蒙系统发布会回放...
  6. Hacked VisualSVN Server by PHP to allow user change password
  7. 线段树-简单线段树模板
  8. 龙芯上市是自主路线被广泛认可的风向标
  9. 蓝桥杯(java)基础练习 龟兔赛跑
  10. postman怎么传session_Day 47: 不搞懂Cookie和session誓不罢休
  11. ggplot2作图详解:图层语法和图形组合
  12. Win 10 搭建 EasyDarwin 流服务器
  13. 想在抖音上卖衣服不知道怎么入手,抖音小店改销量技术
  14. 多传感器融合用卡尔曼滤波的话也逃不开状态方程观测方程
  15. html 调高德地图 导航,在H5页面内通过地址调起高德地图实现导航
  16. 笔记:图解网络(小林coding)
  17. 致80后的北漂IT人:未来在哪儿?
  18. 微信小程序map展示
  19. 轮播图代码,带定时器和小圆圈(易懂)
  20. openldap + samba为openldap添加smb属性----群晖synology

热门文章

  1. Mybatis基础学习之一级缓存和二级缓存的简单使用
  2. VideoScribe基础教程创建动画视频
  3. 简约蓝色好看的域名出售单页源码
  4. 会计行业就业前景分析
  5. Java 中compare方法与equals方法一样,在继承中会遇到问题
  6. Google Gears
  7. vue中阻止冒泡 阻止默认行为
  8. 以太网PHY直接连接
  9. 单木分割实验之距离聚类(实验五)
  10. 【论文笔记】—人脸识别—FaceNet—2015-CVPR