描述

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. Java设计模式——合成/聚合复用原则

    一.什么是合成/聚合复用原则? 合成/聚合复用原则是在一个新的对象里面使用一些已有的对象,使之成为新对象的一部分:新的对象通过向这些对象的委派达到复用已有功能的目的. 简述为:要尽量使用合成/聚合,尽 ...

  2. web安全/渗透测试--1--web安全原则

    web 安全:  https://blog.csdn.net/wutianxu123/article/category/8037453/2 web安全原则 安全应该是系统开发之初就考虑的问题.换句话说 ...

  3. 01_hello world

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 循环取到json中的字段数据,加到html中

    $.ajax({ type:'post', data:{specialName:specialName,count:count}, url:"admin/pcAdminGetArticleL ...

  5. 神经网络中的激活函数具体是什么?为什么ReLu要好过于tanh和sigmoid function?(转)

    为什么引入激活函数? 如果不用激励函数(其实相当于激励函数是f(x) = x),在这种情况下你每一层输出都是上层输入的线性函数,很容易验证,无论你神经网络有多少层,输出都是输入的线性组合,与没有隐藏层 ...

  6. apicloud监听返回键(安卓试过)

    下面监听写在apiready里面 api.addEventListener({ //使用下方函数此处好像失效了 name: 'keyback' }, function(ret, err) { aler ...

  7. linux下面设置密码失效参考

    chage -E 2005-12-31 user1 设置用户口令的失效期限

  8. cakePHP模型内置回调函数afterFind()的使用。

    在用find获取数据后,我们要对所获取到的数据做一些处理,这时,直接在模型层覆盖cakephp内置的回调函数,使用find时会自动调用. 其中$baomings 就是find 到的 $this-> ...

  9. tomcat实现https

    第一步:生成key文件: C:\>keytool -genkey -alias tomcat -keyalg RSA -keystore C:\tomcat.key 密码最好设置默认change ...

  10. linux shell 学习笔记01

    1.命令历史记录history !$     :调用上一条命令的执行结果 !100   :运行history记录里的第100条命令 !ser   :调用以ser开头的最后一次执行的命令 ctrl+r  ...