题目链接

题目

题目描述

In ICPCCamp there were n towns conveniently numbered with \(1, 2, \dots, n\)

connected with (n - 1) roads.

The i-th road connecting towns aia_iai and bib_ibi has length \(c_i\) .

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 \(\delta(x, y)\) cents,

where \(\delta(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.

输入描述

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 aia_iai, bib_ibi and cic_ici.

  • \(1 \leq n \leq 10^5\)
  • \(1 \leq a_i, b_i \leq n\)
  • \(1 \leq c_i \leq 10^8\)
  • The number of test cases does not exceed 10.

输出描述

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

示例1

输入

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

输出

19
15

题解

知识点:DFS,树的直径。

重修 \(n-1\) 条路,使得花费和最大,每条路的花费是两端原先的最短路。

容易证明,只要以树的直径两端为道路的一端连接其他点,即可最大化花费。因此,先通过两次dfs找到树的直径端点,然后遍历每个点取距离最远的一端即可。

时间复杂度 \(O(n+m)\)

空间复杂度 \(O(n+m)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; template<class T>
struct Graph {
struct edge {
int v, nxt;
T w;
};
int idx;
vector<int> h;
vector<edge> e; Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {} void clear(int n, int m) {
idx = 0;
h.assign(n + 1, 0);
e.assign(m + 1, { 0,0,0 });
} void add(int u, int v, T w) {
e[++idx] = edge{ v,h[u],w };
h[u] = idx;
}
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
while (cin >> n) {
Graph<int> g(n, n << 1);
for (int i = 2;i <= n;i++) {
int u, v, w;
cin >> u >> v >> w;
g.add(u, v, w);
g.add(v, u, w);
} function<void(int, int, vector<ll> &)> dfs = [&](int u, int fa, vector<ll> &dis) {
for (int i = g.h[u];i;i = g.e[i].nxt) {
int v = g.e[i].v, w = g.e[i].w;
if (v == fa) continue;
dis[v] = dis[u] + w;
dfs(v, u, dis);
}
}; vector<ll> dis(n + 1);
dfs(1, 0, dis);
int pos1 = 0;
for (int i = 1;i <= n;i++)
if (dis[i] > dis[pos1]) pos1 = i; vector<ll> dis1(n + 1);
dfs(pos1, 0, dis1);
int pos2 = 0;
for (int i = 1;i <= n;i++)
if (dis1[i] > dis1[pos2]) pos2 = i; vector<ll> dis2(n + 1);
dfs(pos2, 0, dis2);
ll ans = dis1[pos2];
for (int i = 1;i <= n;i++) {
if (i == pos1 || i == pos2) continue;
ans += max(dis1[i], dis2[i]);
}
cout << ans << '\n';
} return 0;
}

NC52867 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. python3使用json、pickle和sqlite3持久化存储字典对象

    技术背景 在各种python的项目中,我们时常要持久化的在系统中存储各式各样的python的数据结构,常用的比如字典等.尤其是在云服务类型中的python项目中,要持久化或者临时的在缓存中储存一些用户 ...

  2. ONVIF网络摄像头(IPC)客户端开发—最简RTSP客户端实现

    前言: 网上对于RTSP协议客户端的表述和实现非常不清晰,在实际使用中,FFMPEG和live555这些软件已经实现了RTSP客户端和服务端的所有功能,但是它们并没有将RTSP协议独立出来,通过看li ...

  3. [转帖]细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4

    https://www.cnblogs.com/malecrab/p/5300503.html 1. Unicode与ISO 10646 全世界很多个国家都在为自己的文字编码,并且互不想通,不同的语言 ...

  4. [转帖]SQL SERVER中隐式转换的一些细节浅析

    https://www.cnblogs.com/kerrycode/p/5853257.html 其实这是一篇没有技术含量的文章,精通SQL优化的请绕道.这个缘起于在优化一个SQL过程中,同事问了我一 ...

  5. [转帖]elasticsearch 8.0 linux安装部署

    1. 下载安装包 https://www.elastic.co/cn/downloads/elasticsearch 选择下载linux版本,elasticsearch-8.0.0-linux-x86 ...

  6. [转帖]利用Python调用outlook自动发送邮件

    ↓↓↓欢迎关注我的公众号,在这里有数据相关技术经验的优质原创文章↓↓↓ 使用Python发送邮件有两种方式,一种是使用smtp调用邮箱的smtp服务器,另一种是直接调用程序直接发送邮件.而在outlo ...

  7. Docker镜像精简方法之二 COPY vs ADD 与镜像层

    Docker镜像精简方法之二 COPY vs ADD 与镜像层 摘要 昨天只是讲了一下大体的思路. 但是没有实操. 今天想着修改一下默认的打包镜像的命令,验证一下效果 原始命令 FROM adopto ...

  8. [转帖]Linux如何查看网关地址

      转至:https://baijiahao.baidu.com/s?id=1733537078943023051&wfr=spider&for=pc 服务器之间的通信是通过ip地址来 ...

  9. Istio安装和部署

    Istio的版本对k8s的版本是有要求的,不兼容的版本会引发一些隐蔽的错误,安装前先参考下图 版本 目前支持 发行日期 停止维护 支持的 Kubernetes 版本 未测试,可能支持的 Kuberne ...

  10. Ant Design Vue中Table的选中详解

    <template> <a-table :columns="columns" :data-source="data" :row-selecti ...