描述

As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new highway in your city. Today is your lucky day, because your request was approved. There is one condition though: You must provide the plan for the best highway artery to construct, or else it's not going to happen!

You have a map that shows all communities in your city, each with a unique number, where you may place highway on-ramps. On the map are a set of roadways between pairs of communities, labelled with driving distances, which you may choose to replace with your highway line. Using this network of roadways, there is exactly one route from any one community to another. In other words, there are no two different sets of roadways that would lead you from community A to community B.

You can build a single highway that runs back and forth between any two communities of your choosing. It will replace the unique set of roadways between those two communities, and an on-ramp will be built at every community along the way. Of course, residents of communities that will not have an on-ramp will have to drive to the nearest one that does in order to access your new highway. You know that long commutes are very undesirable, so you are going to build the highway so that longest drive from any community to the nearest on-ramp is minimized. Given a map of your city with the roadways and driving distances, what is the farthest distance from any community that someone would have to drive to get to the nearest on-ramp once your new highway is complete?

输入

The input consists of multiple test cases. Each test case is a description of a city map, and begins with a single line containing an integer N (2 <= N <= 100, 000), the number of communities in the city. Then N-1lines follow, each containing three integers, i; j (1 <= i, j <= n), and d (1 <= d <= 10, 000). Each line indicates that communities i and j are connected by a roadway with driving distance d. Input is followed by a single line with N = 0, which should not be processed.

输出

For each city map, output on a single line the farthest distance from any community to the nearest on-ramp of the new highway.

样例输入

6
2 1 10
3 1 15
1 4 5
4 5 12
4 6 8
0

样例输出

10

题意

给你一个连通图,让你造一条高速(高速必须建在路上,从A到B只有1条路),然后不在高速上的城市的邻近城市必须有高速,求出邻近城市到距离最近的有高速城市的最大距离

题解

邻近城市可以考虑树,求高速肯定是得树上的距离越长越好,可以求出树的直径

答案就是树的直径上的点到所有其他的最短路取最大值

代码

 #include<bits/stdc++.h>
using namespace std; const int maxn=1e5+; vector< pair<int,int> >G[maxn]; int d[maxn],pre[maxn];
int maxx,mpos;
bool vis[maxn]; void dfs(int u)
{
for(int i=;i<(int)G[u].size();i++)
{
int v=G[u][i].first;
int w=G[u][i].second;
if(!vis[v])
{
d[v]=d[u]+w;
if(d[v]>maxx)
{
maxx=d[v];
mpos=v;
}
pre[v]=u;
vis[v]=true;
dfs(v);
} }
} int main()
{
int n;
while(scanf("%d",&n)!=EOF,n)
{
for(int i=;i<=n;i++)d[i]=0x3f3f3f3f,vis[i]=false,G[i].clear();
for(int i=,u,v,w;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
d[]=,vis[]=true,maxx=;
dfs();
for(int i=;i<=n;i++)pre[i]=-,d[i]=0x3f3f3f3f,vis[i]=false;
d[mpos]=,vis[mpos]=true,maxx=;
dfs(mpos);
queue<int>q;
for(int i=;i<=n;i++)d[i]=0x3f3f3f3f;
for(int i=mpos;i!=-;i=pre[i])
{
q.push(i);
d[i]=;
}
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=;i<(int)G[u].size();i++)
{
int v=G[u][i].first;
int w=G[u][i].second;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
q.push(v);
}
}
}
printf("%d\n",*max_element(d+,d++n));
}
return ;
}

TZOJ 3481 Highway Construction(树的直径+最短路)的更多相关文章

  1. XTOJ 1267:Highway(树的直径)***

    http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267 题意:给出一棵树,每条树边有权值,现在要修建n-1条边,边的权值为边 ...

  2. XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]

    这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...

  3. XTU1267:Highway(LCA+树的直径)

    传送门 题意 有n个小镇,Bobo想要建造n-1条边,并且如果在u到v建边,那么花费是u到v的最短路长度(原图),问你最大的花费. 分析 比赛的时候没做出来,QAQ 我们首先要找到树的直径起点和终点, ...

  4. 2017湘潭大学邀请赛H题(树的直径)

    链接:https://www.icpc.camp/contests/4mYguiUR8k0GKE H. Highway The input contains zero or more test cas ...

  5. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  6. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  7. VIJOS1476旅游规划[树形DP 树的直径]

    描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...

  8. poj2631 树的直径

    设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...

  9. 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][St ...

随机推荐

  1. 深度学习原理与框架-卷积神经网络-cifar10分类(图片分类代码) 1.数据读入 2.模型构建 3.模型参数训练

    卷积神经网络:下面要说的这个网络,由下面三层所组成 卷积网络:卷积层 + 激活层relu+ 池化层max_pool组成 神经网络:线性变化 + 激活层relu 神经网络: 线性变化(获得得分值) 代码 ...

  2. python 阿狸的进阶之路(8)

    异常处理 http://www.cnblogs.com/linhaifeng/articles/6232220.html(转) 网络编程socket http://www.cnblogs.com/li ...

  3. APP-2.1-Hbuilder与夜神 & HbuilderX与夜神模拟器连接

    经上一步完成Hbuilder.HbuilderX与夜神模拟器的安装,本次介绍下两者之间的连接设置. 1.三者的安装路径 Hbuilder:E:\SAP UI5\HBuilder HbuilderX:D ...

  4. 回溯法 leetcode题解 Combination Sum 递归法

    题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...

  5. 网关、子网掩码、DHCP, DNS

    都跟ip地址相关,IP地址构成:网络地址+主机地址 子网掩码可以确定网络地址,例如某IP:192.168.1.102 子网掩码:255.255.255.0, 那么网络地址就是192.168.1,主机地 ...

  6. using关键字在C#中的3种用法

    using 关键字有两个主要用途:  (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.  (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. (一).作为指令 1. ...

  7. 将IP地址字符串转为32位二进制

    def str2bin(s): temp = s.split('.') result = '' for i in range(len(temp)): temp[i] = str(bin(int(tem ...

  8. C# 监测每个方法的执行次数和占用时间(测试1)

    在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...

  9. centos 安装python3.6

    环境准备 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 首先去官网下 ...

  10. tensorflow 之tensorboard 对比不同超参数训练结果

    我们通常使用tensorboard 统计我们的accurate ,loss等,并绘制曲线,通常是使用一次训练中的, 但是,机器学习中通常要对比不同的 ‘超参数’给模型训练和预测能力的不同这时候如何整合 ...