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

Hint

The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52. 

Source

题意:求树上最长路。
容易发现:求一个连通块中的最长路径,首先,从该连通块中任意一个结点出发,求最长路径的端点是S,然后再从S出发求最长路径L,路径L就是要求的路径。
代码:
 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int t, n, m;
bool vis[N];
char ss[];
vector<pii> e[N];
queue<int> q;
void add(int u,int v,int w){
e[u].push_back(pii(v,w));
}
int ma;
int f[N];
int s;
void BFS(int x)//BFS求最长路
{
memset(f,, sizeof(f));
memset(vis,, sizeof(vis));
while(!q.empty()) q.pop();
ma=;
q.push(x);
vis[x]=;
s=x;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=;i<e[u].size();i++){
int v=e[u][i].fi;
int w=e[u][i].se;
if(!vis[v]){
if(f[v]<f[u]+w) f[v]=f[u]+w;
vis[v]=;
q.push(v);
}
if(ma<f[v]) ma=f[v],s=v;
}
}
}
int main() { while (scanf("%d%d", &n, &m) == ) {
for(int i=;i<=n;i++) e[i].clear();
for (int i = ; i < m; i++) {
int x, y, z;
scanf("%d %d %d %s", &x, &y, &z, ss);
add(x,y,z),add(y,x,z);
}
BFS();
BFS(s);
pi(ma);
}
return ;
}

POJ1985 树的直径(BFS的更多相关文章

  1. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  2. hdu2196 树的直径 + bfs

    //Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...

  3. 树上选两点(使最短)树的直径+bfs

    题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...

  4. ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

    题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...

  5. luogu P3761 [TJOI2017]城市 树的直径 bfs

    LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...

  6. 树的直径(BFS)

    ][];];];];,,;vis[i]=; ; j <= n ; j++){ ){;//标记 res[j]=res[root]+; ; i <= n- ; i++){; data[b][a ...

  7. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  8. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

  9. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

随机推荐

  1. python中文入库

    http://www.cnblogs.com/evening/archive/2012/04/19/2457440.html 工作需要,将cvm.csv文件中的记录志入数据库, name, owner ...

  2. SQL Server ->> 存储过程sp_rename重命名数据对象

    1) 表转移Schema和重命名表 ALTER SCHEMA Stage TRANSFER dbo.Stage_AAA; EXEC sp_rename 'Stage.Stage_AAA', 'AAA' ...

  3. JAVA利用poi获取world文件内容

    本文主要简单介绍了利用poi包,读取world文件内容. 这个依然存在版本的问题,只能读取doc结尾的老版本文件. 话不多说,上代码: import java.io.File; import java ...

  4. day1 python 介绍、基本语法、流程控制

    请查看我的云笔记链接: http://note.youdao.com/noteshare?id=0ea7425d3e3669800cb0d73f7ec8865d&sub=D87B4BF820C ...

  5. JS 排序:冒泡、 二分搜索 /折半搜索 half-interval search

    冒泡排序:  重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来. var arr = [5,0,-56,900,12]; //大的排序次数 for(var i=0; i& ...

  6. windows 网络通讯模型Overlapped (转)(未看)

    https://blog.csdn.net/jofranks/article/details/7895316 https://blog.csdn.net/caoshiying/article/deta ...

  7. jQuery的datatable的destroy属性,和$("#test").dataTable().fnDestroy();区别,两者的区别

    jQuery的datatable的destroy属性,和$("#test").dataTable().fnDestroy();区别,两者的区别. 1 destroy属性是,销毁实例 ...

  8. html或jsp页面自动提交,无需每次重启服务

    从eclipse转到idea遇到各种问题,之前eclipse可以自动保存页面内容无需重启服务,但是idea不可以,网上找了n种办法也没用,可能版本不一样吧,把我的解决方法纪录一下,方便以后有人遇到这个 ...

  9. Progress

    这个标签用来表示进度,常用来表示下载的进度. <progress value="22" max="100"></progress>   ...

  10. iOS开发中用到的第三方库概览

    前言:记录一下使用过和接触过的第三方库,重要程度与顺序无关 网络请求: AFNetworking:AFNetworking is a delightful networking library for ...