poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985
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.
题意描述:题意很简单,就是求解两个农场之间最远的距离是多少。
算法分析:树的直径。详细分析:hdu4607
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int n,m;
struct node
{
int v,w;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int w)
{
edge[edgenum].v=v ;edge[edgenum].w=w ;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ; edge[edgenum].v=u ;edge[edgenum].w=w ;
edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
} int d[maxn],vis[maxn];
int bfs(int from)
{
memset(d,-,sizeof(d));
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(from);
vis[from]= ;d[from]= ;
int maxlen=-,k=;
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!vis[v])
{
vis[v]=;
d[v]=d[u]+edge[i].w;
Q.push(v);
if (d[v]>maxlen)
{
maxlen=d[v];
k=v;
}
}
}
}
return k;
} int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
int a,b,c;
char str[];
for (int i= ;i<=m ;i++)
{
scanf("%d%d%d%s",&a,&b,&c,str);
add(a,b,c);
}
int v=bfs();
int u=bfs(v);
printf("%d\n",d[u]);
}
return ;
}
poj 1985 Cow Marathon 树的直径的更多相关文章
- BZOJ 3363 POJ 1985 Cow Marathon 树的直径
题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
随机推荐
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- php 购物车的例子
网上搜到的,简单容易理解.cookie存购物车ID,db存购物车数据. //购物车session的产生代码 1 if(! $session && ! $scid) { 2 / ...
- apache2下部署node.js应用程序
版本:apache2.2+node.js(v.10.25) 系统环境:ubuntu 12.04(LTS) 32位 因为有些模块并没有开启 所以需要使用以下命令开启该模块 windows下则直接在htt ...
- 用友二次开发之U810.1销售预订单导入
- python 函数对象(函数式编程 lambda、map、filter、reduce)、闭包(closure)
1.函数对象 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函 ...
- SQL基础篇---函数及其函数配套使用的关键字
一.数值函数 知识点1 SUM 求总和 SELECT breakfast,sum(price) FROM my_foods GROUP BY breakfast ORDER BY SUM(price) ...
- Hive深入浅出
1. Hive是什么 1) Hive是什么? 这里引用 Hive wiki 上的介绍: Hive is a data warehouse infrastructure built on top of ...
- kettle删除资源库中的转换或者作业
在资源库中新建转换,作业都很简单,那么加入现在不需要其中某个转换或者作业该怎么办呢? 下图是已经存在的转换跟作业 现在需要删除aa这个转换 操作步骤如下: 1.工具----资源库----探索资源库 出 ...
- poj 1862 Stripies/优先队列
原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...