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. NOIP 2012 D1T1 Vigenère密码

    嗯嗯 一道找规律的题.... 真佩服那些把表打出来的人 //By SiriusRen #include <cstdio> #include <cstring> using na ...

  2. iOS 应用开发入门指南

    前言:http://www.guomii.com/posts/20250安装工具:http://www.guomii.com/posts/20255工具:http://www.guomii.com/p ...

  3. 第6章 服务模式 在 .NET 中实现 Service Gateway(服务网关)

    上下文 您正在设计企业应用程序,该程序需要使用由其他应用程序提供的服务.该服务定义了一个合约,所有服务使用者要访问该服务都必须遵守该合约.该合约定义了与此服务通信所需的技术.通信协议和消息定义等内容. ...

  4. img和div的宽度不一样问题和li之间空隙问题的解决方案

    img和div宽度不一致问题 今天写代码,遇到一个小问题,我把一张图片放进一个div里,然后没有设置任何的padding和margin,但是发现图片和div的高度不一样,在img的下方出现了3px的空 ...

  5. css 中font属性知识点总结

    一. font属性值可以继承.例如子元素可以继承父元素的行高,字体大小等等. 二.font属性可以进行连写:font: font-sytle  font-weight  font-size/line- ...

  6. 12) 十分钟学会android--APP通信传递消息之简单数据传输

    程序间可以互相通信是Android程序中最棒的功能之一.当一个功能已存在于其他app中,且并不是本程序的核心功能时,完全没有必要重新对其进行编写. 本章节会讲述一些通在不同程序之间通过使用Intent ...

  7. Android Studio ( Linux) 创建模拟器报错

    Linux下Android studio创建模拟器最后一步报错 报错:An error occurred while creating the AVD. See idea.log for detail ...

  8. java是值传递,还是引用传递?

    原文地址:http://blog.csdn.net/zxmzfbdc/article/details/5401960  java到底是值传递,还是引用传递?以前国内的java开发者有过很多争论,由于& ...

  9. 【Oracle】利用trace文件重建控制文件

    重建步骤: 1.生成trace文件: alter database backup controlfile to trace; 2.找到生成的trace文件: show parameter dump u ...

  10. pwiz, a model generator

    文档链接 pwiz is a little script that ships with peewee and is capable of introspecting an existing data ...