题目链接: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 树的直径的更多相关文章

  1. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

  2. poj 1985 Cow Marathon

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 1985 Cow Marathon (树形DP,树的直径)

    题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...

随机推荐

  1. 请注意designer中的NAME field

    If you are trying to set the value of a field through PeopleCode and that field happens to be NAME t ...

  2. 点击后弧形展开的炫酷菜单--第三方开源-- CircularFloatingActionMenu(一)

    CircularFloatingActionMenu在github上项目主页地址:https://github.com/oguzbilgener/CircularFloatingActionMenu ...

  3. jquery实现radio按纽全不选和checkbox全选的实例

    用jquery实现以下两个这个功能: 1.对所有单选按纽中radio全不选 单选按纽:<input type="radio" name="f1">A ...

  4. POJ-1579

    #include <stdio.h> #include <stdlib.h> ][][]; int w(int a,int b,int c){ ||b<=||c<= ...

  5. python2.7爬取豆瓣电影top250并写入到TXT,Excel,MySQL数据库

    python2.7爬取豆瓣电影top250并分别写入到TXT,Excel,MySQL数据库 1.任务 爬取豆瓣电影top250 以txt文件保存 以Excel文档保存 将数据录入数据库 2.分析 电影 ...

  6. uva 11186 Circum Triangle<叉积>

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. Oracle Rac crs无法启动

    OS:ORACLE LINUX 5.7 DB:11.2.0.3 RAC:YES 故障:1.两节点RAC,节点分别为linuxdb1.linuxdb2,其中节点linuxdb2服务器出现故障,无法启动2 ...

  8. Sql Server 语句

    ##目录 #####清除缓存 DBCC FREEPROCCACHE; DBCC DROPCLEANBUFFERS; SELECT stock.IdStock, stock.Descr FROM [In ...

  9. vnextcn

    Flag 标题 通过 提交 AC% 难度   E1 编写Hello World网站 9 17 52% 1   E2 编写Hello World控制台程序 11 13 84% 1   E3 控制器基础练 ...

  10. C++函数传指针和传引用的区别

    从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 而引用是一个别名,它在逻辑上不是独立的,它的存在具有 ...