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. NodeJs安装less(npm方式)

    上一次讲了如何在浏览器端解析less文件,这次是在cmd中使用npm中的less模块来解析 详解如下 首下我们去下载一个https://nodejs.org/en/,   一路next之后,因为文件不 ...

  2. June 10th 2017 Week 23rd Saturday

    A lot of things, we can be touched, but we can not shed tears. 很多事情,我们可以感动,却不能流泪. Sometimes I was to ...

  3. PS软件怎么把视频转成gif动态图?

    PS软件怎么把视频转成gif动态图?Adobe PhotoShop软件的最新版本是可以编辑视频的,并且可以将视频转换为GIF动态图,使用也很简单,下面分享ps制作gif动态图的教程,需要的朋友可以参考 ...

  4. 笔记本win8系统共享wifi上网方法

    华硕笔记本电脑,安装了win8系统,使用wifi上网,由于连接无线路由的机器太多,超过路由连接数上限,因此转为使用笔记本共享wifi方式给手机上网. 最终上网方式为: 笔记本网卡接入无线路由器上网,笔 ...

  5. Android(java)学习笔记9:JDK5之后的Lock锁的概述和使用

    1. Lock锁的概述: java.util.concurrent.locks,接口Lock 首先Lock是一个接口,Lock实现提供了比使用synchronized方法 和 同步代码块更为广泛的锁定 ...

  6. LG3690 【【模板】Link Cut Tree (动态树)】

    题目 终于去写\(LCT\)了 这个大爷讲的挺好的 板子 #include<algorithm> #include<iostream> #include<cstring& ...

  7. MySQL优化 ----开篇

    今天,数据库的操作越来越成为整个应用的性能瓶颈,Mysql优化则是一个经常要谈的问题了. 谈起MySQL优化,咱们先简单谈一下Mysql: Mysql是最流行的关系型数据库管理系统,在WEB应用方面M ...

  8. 【luogu P1231 教辅的组成】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...

  9. Nodejs做整站转发

    刚接触nodejs,做个东西练下手,通过nodejs直接转发整站,原本想把内容全翻译成英文,但google对流量行审查,被封IP,所以就没啥用了, 效果像这样 var b = function (a, ...

  10. C#解析HTML神器 Html Agility Pack

    曾经,我傻乎乎的用正则表达式成功的解析了学校的新闻网.教务管理系统.图书馆管理系统中我想要的所有的内容.那时候废了好大的劲写那正则啊,而且最后还是各种不给力,经常会有意想不到的bug出现,最后经过无数 ...