Highway
Highway |
||
| Accepted : 78 | Submit : 275 | |
| Time Limit : 4000 MS | Memory Limit : 65536 KB | |
HighwayIn 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. InputThe 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 .
OutputFor each test case, output an integer which denotes the result. Sample Input5 Sample Output19 |
//题意:有一棵树,问所有点到这棵树中所有点最远距离和为多少
//因为这个是树,所以,从任意一点 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的更多相关文章
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- [HihoCoder] Highway 高速公路问题
Description In the city, there is a one-way straight highway starts from the northern end, traverses ...
- Total Highway Distance
Total Highway Distance 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a ...
- 基于pytorch实现HighWay Networks之Train Deep Networks
(一)Highway Networks 与 Deep Networks 的关系 理论实践表明神经网络的深度是至关重要的,深层神经网络在很多方面都已经取得了很好的效果,例如,在1000-class Im ...
- Highway Networks
一 .Highway Networks 与 Deep Networks 的关系 深层神经网络相比于浅层神经网络具有更好的效果,在很多方面都已经取得了很好的效果,特别是在图像处理方面已经取得了很大的突破 ...
- Highway Networks Pytorch
导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...
- Highway LSTM 学习笔记
Highway LSTM 学习笔记 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2016-4-5 声明 1)该Dee ...
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- 基于pytorch实现HighWay Networks之Highway Networks详解
(一)简述---承接上文---基于pytorch实现HighWay Networks之Train Deep Networks 上文已经介绍过Highway Netwotrks提出的目的就是解决深层神经 ...
- TZOJ 3481 Highway Construction(树的直径+最短路)
描述 As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new ...
随机推荐
- c# 用OpenXmL读取.xlsx格式的Excel文件 返回DataTable
1.须要引用的dll : DocumentFormat.OpenXml.dll ---须要安装一下OpenXml再引用 WindowsBase ---直接在项目里加入引用 2.方法: /// & ...
- 一个我用来上传代码到Github的 Shell 脚本
因为用git老是要敲许多命令.所以写了个小脚本.代码如下: #! /bin/sh echo Going to simpleWebtest... cd ~/softwaredevelopment/wor ...
- Visual studio C++ MFC的库函数末尾添加“A”和“W”的意义
背景 在使用Visual studio C++ MFC库函数ModifyMenu(...)函数,在Visual studio自动代码补全里提示没有ModifyMenu(...)这个函数,倒是有Modi ...
- windows / Linux 远程桌面访问全面总结 / 共享文件
一般来说,ssh 是指无图形界面形式,是命令行的方式. 速度快. vnc 是的是图形界面形式. 速度慢. ssh 方式登陆: 1.windows ...
- 用JDOM读取XML文件
用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...
- mysql 5.6的安装
MySQL安装 yum install -y perl-Module-Install.noarch cd /usr/local/src wget http://mirrors.sohu.com/m ...
- 转:Window10下RabbitMQ安装图文教程
一.erlang下载安装 1.下载 下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/downloads,双 ...
- 基于tornado实现web camera
基于tornado实现web camera 近期在学习python.找了一个框架学习,我选择的是tornado.由于其不仅仅是一个web开发框架,其还是一个server,异步事件库,一举多得. 我一直 ...
- Atitit. C#.net clr 2.0 4.0新特性
Atitit. C#.net clr 2.0 4.0新特性 1. CLR内部结构1 2. CLR 版本发展史3 3. CLR 2.0 3 4. CLR 4 新特性 概览4 4.1.1. 托管与本地 ...
- DMA摘记
1.特点 PIO模式下硬盘和内存之间的数据传输是由CPU来控制的:而在DMA模式下,CPU只须向DMA控制器下达指令,让DMA控制器来处理数据的传送,数据传送完毕再把信息反馈给CPU,这样就很大程度上 ...