Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4424   Accepted: 2214
Case Time Limit: 1000MS

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.

输的直径,先找到最长路径,然后以最长路径的端点开始找,第二次bfs找到树的直径

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 50100
int head[MAXN],dis[MAXN],vis[MAXN];
int m,n,cnt,Node,ans;
struct node
{
int u,v,val;
int next;
}edge[MAXN*10];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void getmap()
{
char op[2];
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%s",&a,&b,&c,op);
add(a,b,c);
add(b,a,c);
}
}
void bfs(int sx)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
ans=0;
Node=sx;
q.push(Node);
vis[Node]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+E.val;
q.push(E.v);
}
}
}
for(int i=1;i<=n;i++)
{
if(ans<dis[i])
{
ans=dis[i];
Node=i;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
bfs(1);
bfs(Node);
printf("%d\n",ans);
}
return 0;
}

poj--1985--Cow Marathon(树的直径)的更多相关文章

  1. poj 1985 Cow Marathon 树的直径

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

  2. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

  3. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 数组、链表、栈、队列和STL

    数组 数组是一种最基本的数据结构,它是内存上的一块连续存储空间.正因如此数组的随机访问很方便.但数组也有其固有的限制,大小分配后不能改变. STL中的数组 STL中的Array是静态数组模板,就是我们 ...

  2. java三大版本解析

    JAVA三大版本代表着JAVA技术的三个应用领域:JAVASE.JAVAME.JAVAEE. JAVA以前很长一段时间被称为JAVA2,所以现在很多人习惯称为J2SE.J2ME.J2EE,它们表示的含 ...

  3. 相似图像识别检 —基于图像签名(LSH)

    原文链接:http://grunt1223.iteye.com/blog/828192 参考:人工智能,一种现代方法 第 617页,且原始论文给出了完整的证明过程.在ANN方法中,LSH算一种可靠的紧 ...

  4. 几个方便编程的C++特性

    前言: C++11的自动化特性给编程提供了不少方便,同时也给调试增加了很多负担,至于取舍看程序员的风格和侧重而定. auto:自动类型推断 在C++11之前,auto关键字用来指定存储期.在新标准中, ...

  5. C# 遍历对象下的 属性

    foreach (System.Reflection.PropertyInfo p in users.GetType().GetProperties()) { var xx = p.Name; var ...

  6. MySQL主主高可用(keepalive)

    2台新的虚拟机172.16.1.1.172.16.1.2  (配置yum源 ) 安装数据库服务 其中 172.16.1.1.172.16.1.2运行数据库服务并设置数据库管理员从本机登录的密码是xzw ...

  7. Android 将Android项目打包成aar文件

    参考资料:https://blog.csdn.net/csdn_mm/article/details/78364444

  8. Python数据结构2-----队列和堆

    一.线性结构:栈.队列.双端队列.列表 二.非线性结构:树.图.堆 [算法中看堆是非线性的,因为其相当于完全二叉树,但堆的存储元素是采用线性的顺序表数组来实现的] 三.队列: 1.队列类型:FIFO. ...

  9. git 教程2 (git常用命令解说)

    <1>$ git -- help (调出git的帮助文档) <2>$ git +命令 --help (查看某个具体命令的帮助文档) <3>$ git --versi ...

  10. Linux系统下的 /etc/fstab 文件解读

    1 [root@localhost ~]# cat /etc/fstab 2 3 # 4 # /etc/fstab 5 # Created by anaconda on Sat Nov 3 12:03 ...