# Highway

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

• 1n105
• 1ai,bin
• 1ci108
• 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

http://www.dengwenhuo.cn/?id=453

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;

#define ll __int64
const int N=100005;

struct p{
ll to,val;
};

struct pp{
ll u,dis;
bool operator < (const pp&r) const{
return dis>r.dis;
}
};
vector<p>G[N];
int n,vis[N];
ll dis[N];

int dijkstra(int s)
{
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
priority_queue<pp>q;
dis[s]=0;
q.push(pp{s,0});
int k=1;
while(!q.empty())
{
pp now=q.top();
q.pop();
if(vis[now.u]) continue;
vis[now.u]=1;
for(int i=0;i<G[now.u].size();i++)
{
p t=G[now.u][i];
if(dis[t.to]>dis[now.u]+t.val)
{
dis[t.to]=dis[now.u]+t.val;
if(dis[k]<dis[t.to])
k=t.to;
q.push(pp{t.to,dis[t.to]});
}
}
}
return k;
}
void get(int s)
{
memset(vis,0,sizeof(vis));
queue<pp>q;
vis[s]=1;
q.push(pp{s,0});
while(!q.empty())
{
pp now=q.front();
q.pop();
dis[now.u]=max(now.dis,dis[now.u]);
for(int i=0;i<G[now.u].size();i++)
{
p t=G[now.u][i];
if(!vis[t.to])
{
vis[t.to]=1;
q.push(pp{t.to,now.dis+t.val});
}
}
}
}
int main()
{
ll u,v,c;
int k1,k2;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<n;i++)
{
scanf("%I64d %I64d %I64d",&u,&v,&c);
G[u].push_back(p{v,c});
G[v].push_back(p{u,c});
}
k1=dijkstra(1);
ll t=dis[k1];
k2=dijkstra(k1);
t=max(t,dis[k2]);
memset(dis,0,sizeof(dis));
get(k1);get(k2);
ll ans=0;
for(int i=1;i<=n;i++)
ans+=dis[i];
printf("%I64d\n",ans-t);
}
return 0;
}

# Highway

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

• 1n105
• 1ai,bin
• 1ci108
• 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

#### AtCoder - 2334（搜索）

2018-05-22 18:46:57

#### 6480: An Invisible Hand(思维)

2018-05-29 16:56:12

#### 2015弱校联盟(1) - I. Travel

2015-10-01 21:18:18

#### 2017ccpc全国邀请赛(湖南湘潭) H. Highway （最大生成树）（树的直径）

2017-05-23 18:58:57

#### 2017年 湘潭校赛 H题 Highway 最长路径和

2017-05-14 19:58:32

#### 17-JSCPC&&CCPC中南赛区 H-Highway 树的直径DFS

2017-05-22 22:13:52

#### ccpc湘潭邀请赛 h-highway（树的直径）

2017-05-17 16:59:29

#### 湘潭赛的一些感想

2013-05-11 21:16:34

#### 江苏省赛--湘潭杯-highway 题解

2017-06-24 12:22:02

#### 2017 湘潭大学邀请赛/江苏省省赛 H题 Highway

2017-05-21 22:42:24