题目连接

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的更多相关文章

  1. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  2. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

  3. poj:1985:Cow Marathon(求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case ...

  4. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  5. POJ 1985 Cow Marathon(树的直径模板)

    http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

  6. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  7. POJ 1985 Cow Marathon (求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  8. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  9. POJ 1985 Cow Marathon (树形DP,树的直径)

    题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...

随机推荐

  1. asp.net 配置 X-Frame-Options

    近日网站在安全检查,送检的网站被反馈有以下问题 X-Frame-Options Header未配置 漏洞描述: 弱点描述: X-Frame-Options HTTP响应头可以指示浏览器是否允许当前网页 ...

  2. sass mapsource --->gulp

    详细,请戳这里 1.安装插件 npm install --save-dev gulp-sass gulp-sourcemaps gulp-autoprefixer 如果安装错误,请用sudo 权限: ...

  3. CLRS: online maximum (n,k)algorithm

    //the first k elements interviewed and rejected, //for  the latter n-k elements ,if value >max,re ...

  4. 开源项目:DolphinPlayer

    Dolphin Player是一款基于FFmpeg解码视频播放器,支持大多数的音频和视频格式. 项目主页:http://code.google.com/p/dolphin-player/ 源代码Git ...

  5. navicat 连接oracle

    Navicat远程连接oracle数据库数据库需要设置一下oci.dll. 下面介绍如何进行设置. 工具/原料   Navicat Pre instantclient-basiclite-nt-12. ...

  6. 做权限树时 使用EasyUI中Tree

    符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的 [{ "id":1, "text":"My Documents", & ...

  7. hdu2053

    查找1-n中能整除n的数的个数. 如果是偶数的话,结果为0 奇数的话,结果为1 #include <stdio.h> int main(){ int i,cnt,n; while(~sca ...

  8. final 的用法总结

    1.修饰成员变量 修饰普通变量 表明这个变量是一个常量,不可以修改这个变量的值,一般这样的变量的变量名都要大写 修饰引用变量 表明这个引用不能够指向别的对象了,只能够指向指定的这个对象 2.修饰方法 ...

  9. DBLINK 创建与小结

    1.DBLINK 的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数 ...

  10. thinkphp 字段静态验证$_validate中错误提醒多语言化写成{%LANGUATE}的原因

    class UserModel extends Model{ protected $_validate =  array( array('account', 'require', '{%LANGUAG ...