Highway

Accepted : 78   Submit : 275
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

//题意:有一棵树,问所有点到这棵树中所有点最远距离和为多少

//因为这个是树,所以,从任意一点 dfs 搜最远点,再从最远点 dfs 搜最远点,再dfs一下,这样,保存了2个最远点到任意点的距离,遍历一遍选最大的那个即可,最后减去直径,因为重复算了一次

一共就 4 n 次遍历吧,1800 ms 还行吧

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
#define MX 100005
struct To
{
int to;
LL c;
}; int n;
int far;
LL step;
vector<To> road[MX];
LL dis[][MX]; void Init()
{
for (int i=;i<=n;i++)
road[i].clear();
} void dfs(int x,int pre,LL s,int kk)//所在位置,从前,距离,第几次
{
if (kk!=) dis[kk-][x]=s; if (s>step)
{
far=x;
step=s;
}
for (int i=;i<(int)road[x].size();i++)
{
if (road[x][i].to!=pre)
dfs(road[x][i].to,x,road[x][i].c+s,kk);
}
} int main()
{
while (~scanf("%d",&n))
{
Init();
for (int i=;i<n-;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
road[a].push_back((To){b,c});
road[b].push_back((To){a,c});
} step=;
dfs(,,,); step=;
dfs(far,,,);
step=;
dfs(far,,,); LL ans = ;
for (int i=;i<=n;i++)
ans += max(dis[][i],dis[][i]);
ans -= step;
printf("%I64d\n",ans);
}
return ;
}

Highway的更多相关文章

  1. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  2. [HihoCoder] Highway 高速公路问题

    Description In the city, there is a one-way straight highway starts from the northern end, traverses ...

  3. Total Highway Distance

    Total Highway Distance 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a ...

  4. 基于pytorch实现HighWay Networks之Train Deep Networks

    (一)Highway Networks 与 Deep Networks 的关系 理论实践表明神经网络的深度是至关重要的,深层神经网络在很多方面都已经取得了很好的效果,例如,在1000-class Im ...

  5. Highway Networks

    一 .Highway Networks 与 Deep Networks 的关系 深层神经网络相比于浅层神经网络具有更好的效果,在很多方面都已经取得了很好的效果,特别是在图像处理方面已经取得了很大的突破 ...

  6. Highway Networks Pytorch

    导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...

  7. Highway LSTM 学习笔记

    Highway LSTM 学习笔记 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2016-4-5   声明 1)该Dee ...

  8. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  9. 基于pytorch实现HighWay Networks之Highway Networks详解

    (一)简述---承接上文---基于pytorch实现HighWay Networks之Train Deep Networks 上文已经介绍过Highway Netwotrks提出的目的就是解决深层神经 ...

  10. TZOJ 3481 Highway Construction(树的直径+最短路)

    描述 As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new ...

随机推荐

  1. CMake 设置Target输出目录和后缀名

    一. SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin) 上面的语句能设置可执行文件的输出目录 在Win + VS环境下,会自动在你所设置 ...

  2. Keep-Alive简介及在Tomcat中配置

      Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.市场上 的大部分Web服务器,包括iPlanet.IIS和 ...

  3. iOS Core ML与Vision初识

    代码地址如下:http://www.demodashi.com/demo/11715.html 教之道 贵以专 昔孟母 择邻处 子不学 断机杼 随着苹果新品iPhone x的发布,正式版iOS 11也 ...

  4. 使用node.js 文档里的方法写一个web服务器

    刚刚看了node.js文档里的一个小例子,就是用 node.js 写一个web服务器的小例子 上代码 (*^▽^*) //helloworld.js// 使用node.js写一个服务器 const h ...

  5. curl Error : maximum redirects followed , 这种问题的一种原因 .

    在stack overflow 上查找到有些网站上需要返回一些cookie的,所以当我们curl当相应的网站时,必须要将返回的cookie保存起来. $cookie = tempnam (" ...

  6. redis主从和主从切换

    redis数据量增加,导致内存不够用,要迁移分离redis和程序: 1. 在新redis服务器上,启动一个redis实例,配置和master配置一致,不同的是配置文件中修改并启用 slave-read ...

  7. sql中的笛卡尔积

    sql中的笛卡尔积 我们对数据库表进行操作时,经常会对多张表进行关联,多表连接查询大家肯定不会陌生,但是一不小心很容易出来庞大冗余的数据. 笛卡尔积数学概念 笛卡尔积是指在数学中,两个集合X和Y的笛卡 ...

  8. tony_LVS DR模式 RealServer 为 Windows客户端配置

    LVS   DR模式  Windows 2000,Windows2003客户端配置   控制面板-添加硬件-选“是,我已经连接了此设备”点击下一步-在列表中选择添加新的硬件设备-选“安   装我充从手 ...

  9. 利用Python读取文件名并生成txt文件——以图片文件为例

    效果如下: 代码: import os class ReadImageName(): def __init__(self): self.path = '.' def readname(self): f ...

  10. linux中的dd复制命令

    dd命令用于复制文件并对原文件的内容进行转换和格式化处理.dd命令功能很强大的,对于一些比较底层的问题,使用dd命令往往可以得到出人意料的效果.用的比较多的还是用dd来备份裸设备.但是不推荐,如果需要 ...