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. Executors

    提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...

  2. sprak pom

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  3. haproxy和nginx负载均衡分析

    https://my.oschina.net/zhuangweihong/blog/813231

  4. How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.frame];    [backgroundIm ...

  5. Mysql数据库中CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别

    如图所示,mysql数据库中,当字段类型为timestamp时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,end_time的值自动设置为系统当前时间,如果勾选了 ON ...

  6. C#中值类型和引用类型的差别浅记

    C#中值类型和引用类型的差别浅记         在C#中,变量的类型分为两种.各自是值类型和引用类型.         值类型的变量直接存储值,说得更详细一些,就是值类型变量在内存中直接存储它们自身 ...

  7. BeagleBone Black Industrial 工业版介绍

    前言 在电子发烧友论坛看到有Beaglebone Black Industrial版的试用,这里介绍一下这块开发板. BBB是开源硬件,原理图.BOM等都开放下载,所以也有诸多兼容板. BBB兼容产品 ...

  8. 走入asp.net mvc不归路:[3]创建控制器

    实际上,控制器就是一个类,一个继承自Controller的类.正常创建一个Controller即可,而问题在于asp.net mvc提供了一套便捷的方法,在创建一个Controller时,可以自动创建 ...

  9. C#语言基础语句

    case,switch,break的使用 Console.WriteLine("1.汉堡"); Console.WriteLine("2.薯条"); Conso ...

  10. 继承LinearLayout实现根据屏幕宽度及内部子View个数自动排布GridView

    public class VerticalSearchGridView extends LinearLayout implements View.OnClickListener { private i ...