poj 2631 Roads in the North
题目连接
http://poj.org/problem?id=2631
Roads in the North
Description
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
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
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22
树的直径。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::sort;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 11000;
const int INF = 0x3f3f3f3f;
struct edge { int to, w, next; }G[N << 1];
int tot, head[N], dist[N];
void init() {
tot = 0, cls(head, -1);
}
void add_edge(int u, int v, int w) {
G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;
G[tot] = (edge){ u, w, head[v] }; head[v] = tot++;
}
int bfs(int u) {
queue<int> q;
q.push(u);
cls(dist, -1);
int id = 0, maxd = 0;
dist[u] = 0;
while(!q.empty()) {
u = q.front(); q.pop();
if(dist[u] > maxd) {
maxd = dist[id = u];
}
for(int i = head[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(-1 == dist[e.to]) {
dist[e.to] = dist[u] + e.w;
q.push(e.to);
}
}
}
return id;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
init();
int u, v, w;
while(~scanf("%d %d %d", &u, &v, &w)) {
add_edge(u, v, w);
}
printf("%d\n", dist[bfs(bfs(1))]);
return 0;
}
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【树的直径裸题】
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 ...
- 题解报告: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以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- POJ [P2631] Roads in the North
树的直径 树的直径求法: 任取一点u,找到树上距u最远的点s 找到树上距s点最远的点t,s->t的距离即为所求 #include <iostream> #include <cs ...
随机推荐
- 获取oracle 里的表名与字段
--数据库表名 SELECT distinct A.OBJECT_NAME as TAB_NAME,B.comments as DESCR FROM USER_OBJECTS A , USER_TAB ...
- 学习练习 java20160507作业
第一题 求水仙花的个数: //求水仙花数 int zongshu = 0; for(int i =100; i<=999;i++) { int bai = i/100; //求百位上面的数字 i ...
- TCP/IP详解学习笔记(10)-- DNS:域名系统
1.DNS DNS 是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由解析器以及域名服务器组成的.域名服务器是指保存有该网络中 ...
- asp.net Global.asax 方法的使用和说明
Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法你可以使用这个文件实现应用程序安全性以及其它一些任务下面让我们详细看一 ...
- 捣蛋phpwind之WindFrameWork
一直都有关注phpwind这个开源产品,从9.0开始就好关注拉,因为官方说把之前的代码重写了一遍,融入了windFramework这个框架,代码真的挺优美的,今日在做社区的一些功能,心血来潮就参考了p ...
- Xcode Alcatraz插件管理介绍和使用
Xcode Alcatraz插件管理介绍和使用http://www.jianshu.com/p/7a2484123bf6 1.简介 Alcatraz是一个能帮你管理Xcode插件丶模版及颜色配置的工具 ...
- 解决Android sdk更新不能下载或下载内容过慢
Android Android SDK 配置步骤 启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manag ...
- makefile中的target到底代表什么?
刚开始用make的时候,以为makefile中名字叫做main的target就是make默认执行的target(中文翻译叫做标的(di,四声),下面统一称呼为标的),然后曾经很天真地犯过这样的错误,就 ...
- 第 2章 数组和 ArrayLists
数组是最通用的数据结构,它出现在几乎所有的编程语言里.在 C#语言中使用数组包括创建 System.Array 类型的数组对象,以及创建针对所有数组的抽象的基类型.Array 类提供了一套方法,这些方 ...
- ASP.NET状态保持:ViewState
ViewState是ASP.NET的.aspx页面特有的,是页面级的状态保持.一般用在内网系统和网站后台. namespace WebFormTest.TestCollect { public par ...