Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 5362   Accepted: 2634
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. 

Source

 
【思路】
求树的直径。树上最远两点距离。两遍dfs。bfs也可。
W S E N 并没有什么卵用‘
【code】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,x,y,z,head[],dad[],dis[],maxx,maxn,sumedge;
struct Edge
{
int x,y,z,nxt;
Edge(int x=,int y=,int z=,int nxt=):
x(x),y(y),z(z),nxt(nxt){}
}edge[];
void add(int x,int y,int z)
{
edge[++sumedge]=Edge(x,y,z,head[x]);
head[x]=sumedge;
}
void dfs(int x)
{
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(dad[x]!=v)
{
dad[v]=x;
dis[v]=dis[x]+edge[i].z;
dfs(v);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
char s[];
cin>>x>>y>>z>>s[];
add(x,y,z);
add(y,x,z);
}
dfs();
maxx=-0x7fffff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
maxn=i;
}
}
memset(dis,,sizeof(dis));
memset(dad,,sizeof(dad));
dfs(maxn);
maxx=-0x7fff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
}
}
printf("%d\n",maxx);
return ;
}

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. POJ1985Cow Marathon[树的直径]

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5117   Accepted: 2492 Case ...

  4. poj1985 / poj2631(树的直径)

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

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

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

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

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

  7. 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

    3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ​ 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...

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

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

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

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

随机推荐

  1. MySQL什么时候会使用内部临时表?

    1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...

  2. Centos7安装完成后一些小优化

    1.修改ip地址.网关.主机名.DNS等 [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #网 ...

  3. javafx中多场景的切换

    0.前言 前段时间在做javafx的应用程序,遇到一些坑.以本文记录之.(如有更好的解决办法欢迎评论,本人小白,轻喷) 1.问题 按照官方的中文文档,成功的运行了单一界面的表单登录.于是想自己试试多界 ...

  4. Maven配置将war包部署到Tomcat(tomcat7-maven-plugin)

    Tomcat7/8: 提示:经过测试Tomcat7的配置和插件在Tomcat8中能正常运行 tomcat7-maven-plugin官方帮助文档:http://tomcat.apache.org/ma ...

  5. ios Crash Log 分析汇总

    方法一: 1.xcode 有自带的symbolicatecrash,可以将.crash文件中的16进制地址转换成可读的函数地址. symbolicatecrash位于: /Applications/X ...

  6. JS --- 数组循环要用length

    socket.on("receive", function (data) { deviceone.print("返回的数据:"+data) // 发送异常 va ...

  7. Mecanim动画编辑器 - 加入动画层实现并行动作

    1.创建新的状态层 a) 通过下图的1button创建一个新的层   b) 通过下图2属性设置图层的权重.假设为0,则该图层的状态不会影响到总的状态机  c) Mask是设置动画的Avatar的关联节 ...

  8. indexOf 和 lastIndexOf 的区别

    indexOf 和 lastIndexOf 是什么? indexOf 和 lastIndexOf 都是索引文件 indexOf 是查某个指定的字符串在字符串首次出现的位置(索引值) (也就是从前往后查 ...

  9. 配置resin支持maven项目

    1. 在resin.conf中找到 <!-- includes the app-default fordefault web-app behavior --> <resin:impo ...

  10. 几个简单的程序看PHP的垃圾回收机制

    每一种计算机语言都有自己的自动垃圾回收机制,让程序员不必过分关心程序内存分配,php也不例外,但是在面向对象编程(OOP)编程中,有些对象需要显式的销毁,防止程序执行内存溢出. 一.PHP 垃圾回收机 ...