TZOJ 3481 Highway Construction(树的直径+最短路)
描述
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(树的直径+最短路)的更多相关文章
- XTOJ 1267:Highway(树的直径)***
http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267 题意:给出一棵树,每条树边有权值,现在要修建n-1条边,边的权值为边 ...
- XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]
这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...
- XTU1267:Highway(LCA+树的直径)
传送门 题意 有n个小镇,Bobo想要建造n-1条边,并且如果在u到v建边,那么花费是u到v的最短路长度(原图),问你最大的花费. 分析 比赛的时候没做出来,QAQ 我们首先要找到树的直径起点和终点, ...
- 2017湘潭大学邀请赛H题(树的直径)
链接:https://www.icpc.camp/contests/4mYguiUR8k0GKE H. Highway The input contains zero or more test cas ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
- poj2631 树的直径
设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...
- 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1034 Solved: 562[Submit][St ...
随机推荐
- day01-MySQL介绍
一.MySQL的介绍 1.1.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 W ...
- centos7 redis配置
https://www.cnblogs.com/web424/p/6796993.html
- Linux查看当前系统的发行版信息
lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution information.). ...
- 新装kafka与zookeeper配置
zookeeper配置 dataDir=/opt/kafka_2.11-2.0.0/data/zookeeper # 尽量不要放在tmp# the port at which the clients ...
- UnityHub破解
1.退出UnityHub,安装好nodejs执行以下命令 npm install -g asar 2.打开UnityHub安装目录如 C:\Program Files\Unity Hub\resour ...
- JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)
创建数据库和表 首先,创建一个web项目 然后引入jar包(jstl.jar和standard.jar是jstl和el包,在jsp页面中需要手动加 <%@ taglib uri="ht ...
- GIS工具-shp浏览器
GIS工具-shp浏览器 软件特点: 1. 单个文件,windows平台 2. 绿色,不用安装 3.C语言系列开发,非vb,.net,Java等,无需虚拟机,无需运行时,无需第三方工具 获取方法: 十 ...
- Linux下设置动态库的方法
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的. 一般 Linux 系统把 /lib 和 /usr/lib 两个目录作为默认的库搜索路径,所以使 ...
- HTML css 样式表
CSS样式表 2.1.样式表的基本概念 2.1.1.样式表分类 1.内联样式表 和html联合显示,控制精确,但是可重用性差,冗余多. 例:<p style="font-size:14 ...
- ADO.Net创建数据模型和数据访问类及泛型集合
数据模型和数据访问类:数据模型: 使用面向对象中的封装特性,将数据表中的行数据组成一个同样结构的对象,来单独使用: 数据访问类: 将某一个表的全部增删改查操作的方法写进去,方便统一管理和调用: 数据模 ...