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 ...
随机推荐
- EF CRUD 操作
1.Add 操作 public bool Add(EFDataModels.User model) { try { int result=0; using (DBEntities db = new D ...
- UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推
https://vjudge.net/problem/UVA-11054 As you may know from the comic “Asterix and the Chieftain’s Shi ...
- shell脚本循环执行mysql语句
参考资料:Shell脚本中执行mysql语句 需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分. 解决方法: ...
- Discussing the scenery in the program of 863 with Doctor Zhang!
今天,下午去了NEC找章丰博士师兄交流了一下863项目关于SDN场景的设置问题,通过交流感觉师兄的水平和层次完全在另一个层次,以及人家的谈吐. 主要的结论有以下几个:(1)移动性管理场景 (2)特殊 ...
- CLRS:Max_sunsequence_sum O(n*n) O(nlgn) O(n)
#include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf ...
- 使用ffmpeg快速生成视频截图
1 ffmpeg -i input.mkv -ss 00:10:00 -f image2 output.jpg 但是这个命令会花费相当长的时间. 对一个清晰的或者较大的视频文件进行操作, 会花费半分钟 ...
- rel="stylesheet" 描述
<link type="text/css" rel="stylesheet" href="css/style.css"/> re ...
- Threatening letter in Naver Line App
A suspect sent a threatening letter in Naver Line App to Richman, and said that he wanted those mone ...
- wifi热点共享 3G网络
本文描述一些iptables的基础知识和使用方法.最后记录一个wifi共享3G上网,以及禁止wifi内某个IP通过3G联网的例子. 一. 在Linux系统中,防火墙,网址转换(NAT),数据包记录以及 ...
- 使用C#三维绘图控件快速搭建DXF查看程序
本例使用AnyCAD .Net三维图形控件快速实现一个DXF文件的读取.显示.导出JPG.PNG.PDF的应用. 代码: using System; using System.Collections. ...