poj 1985 Cow Marathon
题目连接
http://poj.org/problem?id=1985
Cow Marathon
Description
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms.
Input
* Lines 1.....: Same input format as "Navigation Nightmare".
Output
* Line 1: An integer giving the distance between the farthest pair of farms.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
Sample Output
52
树的直径。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::find;
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 = 50010;
const int INF = 0x3f3f3f3f;
struct work {
struct edge { int to, w, next; }G[N << 1];
int tot, head[N], dist[N];
inline void init() {
tot = 0, cls(head, -1);
}
inline 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++;
}
inline int bfs(int s) {
int id = s, max_dist = 0;
cls(dist, -1);
queue<int> q;
q.push(s);
dist[s] = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
if(dist[u] > max_dist) {
max_dist = 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;
}
inline void solve(int m) {
char ch;
int u, v, w;
cls(head, -1);
while(m--) {
scanf("%d %d %d %c", &u, &v, &w, &ch);
add_edge(u, v, w);
}
printf("%d\n", dist[bfs(bfs(u))]);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while(~scanf("%d %d", &n, &m)) {
go.solve(m);
}
return 0;
}
poj 1985 Cow Marathon的更多相关文章
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
随机推荐
- Eclipse SVN冲突解决
基本原则是:每次提交前需要先和线上的对比,先把冲突解决掉,然后把线上的更新到本地,最后把本地的提交上去. 右键项目 -> Team -> 与资源库同步 在同步视图中选择Conflicts ...
- 学习总结 java 父子级
package com.hanqi; //父类 public class Father { // public Father() // { // // } // public Father(Strin ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- dell 交换机 双链路冗余
公司海外机房引入2G带宽,是由2个电口绑定实现的.因业务需要扩容到3G,在绑定端口扩展性不太好,因此直接上10G光纤模块. 机房技术人员建议,2g老线路不撤做备份,3g新线路在线使用.使用STP协议实 ...
- 转载 《AngularJS》5个实例详解Directive(指令)机制
<AngularJS>5个实例详解Directive(指令)机制 大漠穷秋 本文整理并扩展了<AngularJS>这本书第六章里面的内容,此书近期即将由电子工业出版社出版,敬请 ...
- Gestures_Article_4_0
Technical Article Windows Phone 7™ Gestures Compared Lab version: 1.0.0 Last updated: August 25, 201 ...
- Android IOS WebRTC 音视频开发总结(五三)-- 国内IM & RTC SDK列表
本文主要总结国内提供RTC SDK的产品,转载必须说明出处,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help 自从开通邮件和微信公众号以来,很多 ...
- linq to xml 基本操作
使用XDocument类来进行简单的xml操作,用于软件参数的设置保存. using System; using System.Collections.Generic; using System.Li ...
- C++ 必知必会: 条款1: 数据抽象
数据抽象, 具有C语言经理的我, 原先是将其看做是一个数据的集合, 把自己常用的数据通过某种方式总结,集合起来, 使用的时候能够更加方便------其实这个还是结构体思想的延续,结构体内部包含了若干的 ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...