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. oc 获取当前时间

    //获取当前日期 NSDate* date = [NSDate date]; NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; ...

  2. C# 匿名表达式(Lambda表达式)

    匿名表达式 这次来说说Lambda表达式吧,反正也简单,我也不像其他高手那样强调来强调去,只讲一下方法: 准备条件如下: 第一,匿名表达式必须存在与之对应的委托. 只要存在相对应的委托就可以了.接下来 ...

  3. Codevs 1082 线段树练习 3

    1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Maste 传送门 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的 ...

  4. C语言基础程序设计

    1 概论 程序(指令和数据的集合)在运行时,首先会被加载到内存(此时称为进程),然后由CPU通过控制器的译码从内存中读取指令,并按照指令的要求,从存储器中取出数据进行指定的运算和逻辑操作等加工,然后再 ...

  5. C++ xmmp IM开发笔记(一)

    XMMP C++库采用 gloox (client) 下载地址:http://camaya.net/download/gloox-1.0.11.tar.bz2 glooxd (server) 下载地址 ...

  6. new作为修饰符

    new 修饰符与 new 操作符是两个概念 new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员.而new 操作符用于实例化一个类型 new 修饰符只能用于继承类,一般用于弥补基类设计的不足 ...

  7. 读书笔记-《Training Products of Experts by Minimizing Contrastive Divergence》

    Training Products of Experts by Minimizing Contrastive Divergence(以下简称 PoE)是 DBN 和深度学习理论的 肇始之篇,最近在爬梳 ...

  8. xadmin的插件机制

    xadmin的视图方法中如果加了@filter_hook 标记的都可以作为插件的钩子函数. 例如在ListAdminView类中有许多加了上述标记的方法, @filter_hook def get_c ...

  9. Xcode 插件开发

    我最近一年来都在开发ios应用,不过感觉公司的app维护起来非常麻烦. 因为公司要为很多个企业订做app,每个app的功能基本相同,只是界面上的一些图片和文字要换掉,功能也有一些小改动.考虑到代码维护 ...

  10. Linux Kernel Makefile Test

    一.本文说明 本文为linux内核Makefile整体分析的续篇,是依据Linux内核Makefile体系的主要内容编写一个简要的测试工程.Linux内核Makefile体系就好像一只“大鸟”,而这篇 ...