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. HTTP Strict Transport Security

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. 作用 一个网站接受一个HTTP的 ...

  2. Struts的学习-例子

    一.新建空项目user和配置maven实现下面的页面 1.配置内容 2.编写struts.xml实现页面 <!--定义一个useraction--> <package name=&q ...

  3. day6-基础 模块详解

    1.定义: 1)模块:本质上就是一个python程序,封装成一个"模块",可以调用里面的方法,用来实现一个功能.逻辑上用来组织python代码. 2)包:本质上是一个目录(必须带有 ...

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

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

  5. C语言socket编程----struct sockaddr 和struct sockaddr_in介绍和初始化

    sockaddr结构体 struct  sockaddr{ sa_family_t  sa_family; //地址族,最常用的是"AF_INET"(IPV4)和"AF_ ...

  6. 失去光标display=none事件的坑

    1.实现效果: 失去光标进行判断,如果内容为空出现提示. 2.页面代码: <tr class="tableform_tr"> <td width="15 ...

  7. Android(java)学习笔记34:Android原理揭秘系列之View、ViewGroup

    1. 作过Android 应用开发的朋友都知道,Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的 ...

  8. xHTML与HTML的写法有什么不同?

    全部标签都必须小写 在XHTML中,全部的标签都必须小写.不能大写和小写穿插当中.也不能全部都是大写. 事比例如以下. 错误:<Head></Head><Body> ...

  9. 贪心算法,今年暑假不AC

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2037 活动安排问题,可用贪心. 1.把活动按结束时间递增排序. 2.直观上,选择相对活动为未安排活动留 ...

  10. mysql中set和enum使用(简单介绍)

    简单介绍 SET类型 在创建表时,就指定SET类型的取值范围. 属性名 SET('值1','值2','值3'...,'值n') 其中,“属性名”参数指字段的名称:“值n”参数表示列表中的第n个值,这些 ...