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. 教妹学 Java:大有可为的集合

    00.故事的起源 “二哥,上一篇<泛型>的反响效果怎么样啊?”三妹对她提议的<教妹学 Java>专栏很是关心. “有人评论说,‘二哥你敲代码都敲出幻想了啊.’” “呵呵,这句话 ...

  2. SpringBoot中mybatis的自动生成

    1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...

  3. Ubuntu 16.04安装Mac OS 12虚拟机资源(没成功,但资源还是可以用)

    整理的Mac OS 12虚拟机资源.装虚拟机基本是按这样的套路: 1.先装VM 2.破解VM使其支持Mac OS 12,这个脚本基本是全平台支持,可以看里面的教程文档. 3.用镜像安装系统. 资源: ...

  4. hdu1385Minimum Transport Cost(最短路变种)

    题目链接: huangjing 思路: 输出路径的最短路变种问题..这个题目在于多组询问.那么个人认为用floyd更加稳妥一点.还有就是在每一个城市都有过路费,所以在floyd的时候更改一下松弛条件就 ...

  5. PHP网站渗透中的奇技淫巧:检查相等时的漏洞

    PHP是现在网站中最为常用的后端语言之一,是一种类型系统 动态.弱类型的面向对象式编程语言.可以嵌入HTML文本中,是目前最流行的web后端语言之一,并且可以和Web Server 如apache和n ...

  6. HDU 1501

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. C标准提前定义宏,调试时加打印非常实用

    #include<stdio.h> int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE_ ...

  8. C++类型的转换

    C风格转换是“万能的转换”,但需要程序员把握转换的安全性,编译器无能为力:static_cast最接近于C风格转换,但在无关类指针转换时,编译器会报错,提升了安全性:dynamic_cast要求转换类 ...

  9. Office转SWF的一些感想(Office2007和Office2010)

    Office2007需要借助SaveAsPDFandXPS的插件完成,Office2010可以直接兼容. Office2PDF主要采用的是 Microsoft.Office.Interop的方式进行, ...

  10. bzoj1601【Usaco2008 Oct】灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1589  Solved: 1035 [Submit][ ...