题解报告:poj 2631 Roads in the North(最长链)
Description
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
AC代码:
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=1e4+;
struct node{int to,cap,next;}edge[maxn<<];
int x,y,w,cnt,maxdist,head[maxn];
void add_edge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u,int fa,int &maxdist){
int Dmax=,Dsec=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
int nowd=dfs(v,u,maxdist)+edge[i].cap;
if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
else if(nowd>Dsec)Dsec=nowd;
}
}
maxdist=max(maxdist,Dmax+Dsec);
return Dmax;
}
int main(){
memset(head,-,sizeof(head));maxdist=cnt=;
while(~scanf("%d %d %d",&x,&y,&w)){
add_edge(x,y,w);
add_edge(y,x,w);
}
dfs(,-,maxdist);
printf("%d\n",maxdist);
return ;
}
题解报告:poj 2631 Roads in the North(最长链)的更多相关文章
- 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
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- 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 (求树的直径)
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以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- 题解报告:hdu 4607 Park Visit(最长链)
Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...
随机推荐
- Handler之IdleHandler
MessageQueue提供了另一类消息,IdleHandler 如果返回false,每次轮询都会调用(理论上应该可以做一些别的东西) Looper.myQueue().addIdleHandler( ...
- mysql创建 存储过程 并通过java程序调用该存储过程
create table users_ning(id primary key auto_increment,pwd int); insert into users_ning values(id,123 ...
- js中一些常见写法的含义
1. 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号 ...
- MYSQL初级学习笔记七:MySQL中使用正则表达式!(视频序号:初级_44)
知识点九:MySQL中使用正则表达式(44) (1):REGEXP‘匹配方式’: (2):常用匹配方式: 模式字符 ^ 匹配字符开始的部分 $ 匹配字符串结尾的部分 . 代表字符串中的任意一个字符,包 ...
- hdu acm 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191 题目意思:有 资金 n 和 m 种类型的大米,对第 i 种类型的大米,价格.数量.袋数分别是: ...
- 原:maven+springMVC+mybatis+junit详细搭建过程
阅读目录 1. 工程目录结构整理清楚 2. 引入依赖包 3. 配置数据库连接属性 4. 配置spring配置文件 5. java代码编写(model,dao,service层代码) 6. m ...
- iOS 开发图片资源选择png格式还是jpg格式
对于iOS本地应用程序来说最简单的答案就是始终使用PNG,除非你有非常非常好的理由不用它. 当iOS应用构建的时候,Xcode会通过一种方式优化.png文件而不会优化其它文件格式.它优化得相当的好 他 ...
- 「UVA524」 Prime Ring Problem 质数环
Description 输入正整数n,把整数1,2,-,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.n<=16. A ring is com ...
- 转载:SharePoint:扩展DVWP - 第1部分:布局增强 – 在默认值模板和编辑模板中重新排列栏
SharePoint:扩展DVWP - 第1部分:布局增强 – 在默认值模板和编辑模板中重新排列栏 当我们在数据视图中启用编辑,删除模式的链接时,SPD总是将链接添加到左边. 而我本来希望添加到右侧. ...
- 如何用JavaScript实现获取验证码的效果
转自:http://www.php.cn/js-tutorial-411734.html HTML部分: 1 2 3 4 5 6 7 <body onload='createCode()'> ...