Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4185   Accepted: 2118
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
昨天比赛遇到一个树的直径的题 不知道是什么 唉 坑啊,今天赶紧看看
题意:N个点M条边,每条边都有权值,让你找到两点,使得这两点间所有边的权值之和最长,
(这条路径上任意节点不能有岔路(此处不能有岔路指的是岔路上的边的权值不能算上))
题解:运用两次bfs求最长距离 第一次先任选一点,找到这一点可以到达的最长距离的节点u处
然后再以u点为起点,找到最长路
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 100000
using namespace std;
int head[MAX];
int vis[MAX],dis[MAX];
int n,m,ans;
int sum,aga;
char s[30];
struct node
{
int u,v,w;
int next;
}edge[MAX]; void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int i,j;
int a,b,c;
ans=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d%d%s",&a,&b,&c,s);
add(a,b,c);
add(b,a,c);
}
} void bfs(int beg)
{
queue<int>q;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
int i,j;
while(!q.empty())
q.pop();
aga=beg;
sum=0;
vis[beg]=1;
q.push(beg);
int top;
while(!q.empty())
{
top=q.front();
q.pop();
for(i=head[top];i!=-1;i=edge[i].next)
{
if(!vis[edge[i].v])
{
dis[edge[i].v]=dis[top]+edge[i].w;
vis[edge[i].v]=1;
q.push(edge[i].v);
}
}
} for(i=1;i<=n;i++)
{
if(sum<dis[i])
{
sum=dis[i];
aga=i;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getmap();
bfs(1);
bfs(aga);
printf("%d\n",sum);
}
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 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  4. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

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

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

  6. poj 1985 Cow Marathon

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

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

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

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

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

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

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

随机推荐

  1. KVC 和 OC字典

    KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCo ...

  2. 关于通过bindService启动的service,在unbindService后service是否继续运行的讨论

    有三种情况:如果直接使用服务,则没有必要进行绑定,但是如果要使用服务里面的方法,则要进行绑定.具体的启动情况有下: 1.当启动时,单独调用bindService方法,在unbindService后,会 ...

  3. C++专题 - Qt是什么

    Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程式,也可用于开发非GUI程式,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称 ...

  4. bootstrap上传表单的时候上传的数据默认是0 一定要小心

    bootstrap上传表单的时候上传的数据默认是0 一定要小心

  5. ES6-个人学习笔记一--let和const

    es6已经推出一段时间了,虽然在兼容和应用上还有不少的难题,但是其提供的未来前端代码编程的发展趋势和一些好用的功能还是很吸引人的,因此个人买了'阮一峰'先生的es6入门,希望对其有一个了解和学习,本系 ...

  6. Centos6.5最小化安装:配置网络和自启动服务

    参考http://www.111cn.net/sys/CentOS/56456.htm 1.开启网络连接,禁止IPV6启用 1.开启网络连接 vi  /etc/sysconfig/network-sc ...

  7. Android PackageManager packages.xml文件格式

    packages.xml文件存放在/data/system目录下    该文件记录了系统中所有应用程序的包管理相关信息    PmS根据该文件进行包管理的各种操作 标签名称 所包含的值举例 last- ...

  8. python中的buildin函数详解(第一篇)

    这会是很长的一个帖子,因为我打算从python最基础的东西开始,尝试去完全的掌握它,buildin中有一些常用的函数比如 abs, open, setattr, getattr, 大家都很了解他们的用 ...

  9. auto_ptr, which can release the space automatically

    C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理. 使用std::auto_ptr,要#include <memory>.[1]  中文名 自动指针 外 ...

  10. linux下VI编辑器的使用

    一.VI编辑器简述       VI 编辑器是Linux和Unix上最基本的文本编辑器,工作在字符模式下.由于不需要图形界面,使它成了效率很高的文本编辑器.尽管在Linux上也有很多图形界面的编辑器可 ...