Cow Marathon

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.

题意:给你一棵树,求树上的最长路(也就是直径)。


思路:一开始看见输入后面还有方向,顿时懵了。想了想,给了方向,但并没有路也是连不上的。剩下的两次BFS就OK了。(题目的数据范围在前一道题上),数组一定要开到数据范围的2倍!!(有向边,一开始没有注意到。G++无限RE,C++无限WA)


奉上代码。

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int tot=1,n,m,next[80005],first[40005],w[80005],vis[80005],v[80005],maxx,k;
void BFS()
{
queue<int> q;
memset(vis,0,sizeof(vis));maxx=0;
q.push(k);
while(!q.empty())
{
int t=q.front();q.pop();
for(int i=first[t];i;i=next[i])
if(!vis[v[i]]&&v[i]!=k)
vis[v[i]]=vis[t]+w[i],q.push(v[i]);
}
for(int i=1;i<=n;i++)
if(vis[i]>maxx)
maxx=vis[i],k=i;
}
int main()
{
int x;char s;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d %c",&x,&v[tot],&w[tot],&s);
next[tot]=first[x];first[x]=tot++;w[tot]=w[tot-1];
v[tot]=x;next[tot]=first[v[tot-1]];first[v[tot-1]]=tot++;
}
k=1,BFS(),BFS();
printf("%d",maxx);
}



2016.10.23补充:

这里有一种DP做法

维护一个子树向下的最长链和次长链 (路径没有交)

f[x]表示最长链 g[x]表示次长链 ans=max(ans,f[x]+g[x]);

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
int tot=1,n,m,next[80005],first[40005],w[80005],vis[80005],v[80005],ans;
int f[80005],g[80005];
void dfs(int x,int fa){
for(int i=first[x];i;i=next[i]){
if(v[i]==fa)continue;
dfs(v[i],x);
if(f[v[i]]+w[i]>f[x])g[x]=f[x],f[x]=f[v[i]]+w[i];
else if(f[v[i]]+w[i]>g[x])g[x]=f[v[i]]+w[i];
}
ans=max(ans,f[x]+g[x]);
}
int main(){
int x;char s;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d %c",&x,&v[tot],&w[tot],&s);
next[tot]=first[x];first[x]=tot++;w[tot]=w[tot-1];
v[tot]=x;next[tot]=first[v[tot-1]];first[v[tot-1]]=tot++;
}
dfs(1,-1);
printf("%d",ans);
}

POJ 1985 求树的直径 两边搜OR DP的更多相关文章

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

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

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

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

  3. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  4. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  5. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  6. URAL 1145—— Rope in the Labyrinth——————【求树的直径】

    Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  7. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  8. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  9. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

随机推荐

  1. 单实例redis分布式锁的简单实现

    redis分布式锁的基本功能包括, 同一刻只能有一个人占有锁, 当锁被其他人占用时, 获取者可以等待他人释放锁, 此外锁本身必须能超时自动释放. 直接上java代码, 如下: package com. ...

  2. IntentService和HandlerThread的使用以及源码阅读

    使用MyIntentService.java public class MyIntentService extends IntentService { /** * 是否正在运行 */ private ...

  3. 学不好Python?我们分析看看正确的学习方法是什么-马哥教育

    提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...

  4. elasticsearch学习(1)简单查询与聚合

    elastic 被用作全文搜索.结构化搜索.分析以及这三个功能的组合 一个ElasticSearch集群可以包含多个索引, 每个索引包含多个类型 一个类型存储着多个文档 每个文档又有多个属性 索引(名 ...

  5. Problem 29

    Problem 29 Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 仔细看看以下a与b的组合 22=4, 2 ...

  6. 【5】Django项目配置settings.py详解

    夫唯不争,故天下莫能与之争 --老子<道德经> 本节内容 1.项目配置文件settings.py介绍 2.数据库配置[MySQL] 3.创建模型对象并和数据库同步 4.python官方提供 ...

  7. 第三节:numpy之数组数学运算

  8. SCI 论文金句

    SCI 不会写?其实英语基础好一点,文献多看一点,多写写自然就能自己写出来了.当然,你肯定会说英语真的好难,好吧,就知道你们懒得学英语了.我给你们整理了一套万能模板,涵盖了论文不同部分的常用句型. 摘 ...

  9. 00105_UDP和TCP协议

    1.UDP协议 (1)UDP是User Datagram Protocol的简称,称为用户数据报协议: (2)UDP是无连接通信协议,即在数据传输时,数据的发送端和接收端不建立逻辑连接: (3)当一台 ...

  10. C#实现所有经典排序算法汇总

    C#实现所有经典排序算法1.选择排序 class SelectionSorter { private int min; public void Sort(int[] arr) { ; i < a ...