Roads in the North POJ - 2631
Roads in the North POJ - 2631
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Output
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22 题意:找出最长路
题解:树的直径板子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+;
const int INF=0x3f3f3f3f; struct Edge{
int v,l,next;
}edge[maxn<<];
int vis[maxn],d[maxn],head[maxn],k,node,ans; void init(){
k = ;
memset(head,-,sizeof head);
} void addedge(int u,int v,int l){
edge[k].v = v;
edge[k].l = l;
edge[k].next = head[u];
head[u] = k++; edge[k].v = u;
edge[k].l = l;
edge[k].next = head[v];
head[v] = k++;
}
void dfs(int u,int t){
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].v;
if(vis[v] == )
{
vis[v] = ;
d[v] = t + edge[i].l;
if(d[v] > ans)
{
ans = d[v];
node = v;
}
dfs(v,d[v]);
}
}
}
int main()
{
init();
int l,r,len;
while(scanf("%d%d%d",&l,&r,&len) == )
addedge(l,r,len);
memset(vis,,sizeof vis);
vis[] = ;
ans = ;
dfs(,); memset(vis,,sizeof vis);
vis[node] = ;
ans = ;
dfs(node,); printf("%d\n",ans);
};
Roads in the North POJ - 2631的更多相关文章
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- Roads in the North(POJ 2631 DFS)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
随机推荐
- mysql数据库忘记密码时如何修改(二)
第一步:找到mysql数据库的my.ini配置文件,在[mysqld]下面添加一行代码:skip-grant-tables 第二步:运行services.msc进入服务管理界面,重启mysql服务. ...
- easyUI 鼠标悬停某行提示
最近参与公司的电子档案系统的开发,需求是需要用户鼠标悬停某一行时,需要根据后台业务数据进行提示. 档案系统开发采用的框架是struts2+mybatis+spring+easyUI开发,而前端的e ...
- java 从List<Integer> 中随机获取6个数
List<Integer> list 为不重复的数字集合,例如:1,2,3,4,5,6,7,8,9,10 从中随机获取不重复的6个数.代码如下. List<Integer> l ...
- Js常见算法实现汇总
/*去重*/ <script> function delRepeat(arr){ var newArray=new Array(); var len=arr.length; for(var ...
- qrcode.js的识别解析二维码图片和生成二维码图片
qrcode只通过前端就能生成二维码和解析二维码图片, 首先要引入文件qrcode.js,下载地址为:http://static.runoob.com/download/qrcodejs-04f46c ...
- Int与String之间相互转换
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- 使用JAVA读写Properties属性文件
使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...
- Oracle SQL Developer-3.2.20.09.87 Windows 10启动问题处理&配置
用了好多年的工具,准备在笔记本上使用时启动不了,但在办公室PC上可以正常使用.两者电脑OS都一样,一个是全新安装.一个是从Windows 7升级而来.下载了最新版发现版本到17了,Oracle刷版本号 ...
- String.format()的用法
string.format()用法 2011-06-21 14:58:57| 分类: 工作笔记 | 标签:string format用法 |字号大中小 订阅 1.格式化货币(跟系统的环境有关 ...
- mac 查看python安装路径
1.terminal : input: which Python 2.terminal: input : python --->import sys ----> print sys.p ...