描述

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. Rust语言学习笔记(5)

    Structs(结构体) struct User { username: String, email: String, sign_in_count: u64, active: bool, } let ...

  2. mssqlservers数据嗅探

    SQL Server - 最佳实践 - 参数嗅探问题 转.   文章来自:https://yq.aliyun.com/articles/61767 先说我的问题,最近某个存储过程,暂定名字:sp_a ...

  3. HTTPS好文推荐

    认真看完这几篇文章,HTTPS相关内容应该就能大概了解了. 1.https(ssl)协议以及wireshark抓包分析与解密 2.数字证书原理 3.也许,这样理解HTTPS更容易 4.SSL/TLS原 ...

  4. Linux awk命令使用方法

    awk是linux上非常好用的文本处理工具,常用于指定列的处理,包括获取指定列的内容.根据指定列匹配关系输出等文本处理.本文主要描述awk命令的基本语法.正则表达式与操作符的使用.常用内置变量的含义和 ...

  5. 构造函数,C++内存管理,内存泄漏定位

    构造函数 1.构造顺序 虚基类构造函数,基类构造函数,类对象构造函数,自己的构造函数 2.必须使用初始化列表 (1) 引用成员,常量成员: (2) 基类没默认构造函数(自己重载覆盖了), (3)类对象 ...

  6. 【剑指offer】将字符串中的空格替换成"%20"

    #include <iostream> #include <string> using namespace std; char *ReplaceSpace(char *str, ...

  7. httpClient 深入浅出~

    本文偏重使用,简单讲述httpclient,其实在网络编程中,基于java的实现几乎都是包装了socket的通信,然后来模拟各种各样的协议:httpclient其实就是模拟浏览器发起想服务器端的请求, ...

  8. ArcGIS案例学习笔记-找出最近距离的垂线

    ArcGIS案例学习笔记-找出最近距离的垂线 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:对于任意矢量要素类,查找最近距离并做图 数据: 方法: 0. 计算 ...

  9. C# 中文判断

    原地址忘了,也是博客园的 /// <summary> /// 是否是 中文 /// </summary> /// <param name="CString&qu ...

  10. Docker网络及命令

    Docker常用命令 docker version #查看版本 docker search centos #搜索可用docker镜像 docker images 查看当前docker所有镜像 dock ...