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. SpringMVC参数绑定、Post乱码解决方法

    从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...

  2. Linux快速入门教程-进程管理ipcs命令学习

    使用Linux系统必备的技能之一就是Linux进程管理,系统运行的过程正是无数进程在运行的过程.这些进程的运行需要占用系统的内存等资源,做好系统进程的管理,对于我们合理分配.使用系统资源有非常大的意义 ...

  3. 基于fpga uart学习笔记

    2018年7月24日 uart 接收 部分测试成功,多谢开源骚客 邓堪文老师 ,想学的同学可以微信公众号搜索开源骚客 好啦!言归正传. 1.先附上老师的时序图,自己有点懒不想画,rx_t.rx_tt. ...

  4. DOCKER - POD操作

    强制删除 Terminating 的pod kubectl delete  -n <namespace> <pod podname> --grace-period=0 --fo ...

  5. DOCKER - 容器抓包

    https://help.aliyun.com/knowledge_detail/40564.html?spm=a2c4e.11153940.blogcont272172.10.b09e28a6AOd ...

  6. SolidWorks学习笔记(一)

    一.草图绘制 1.简单命令 先直线后圆弧,几何约束.尺寸标定 2.圆周阵列 3.几何关系——对称 添加几何约束:圆弧相等.关于竖直中心线的对称 4.捕捉圆心 5.尺寸锁定,有圆弧的,先直后圆 6. 7 ...

  7. EF 配置

    DbContext public class ZSZDbContext : DbContext { //ILog ILogger 容易混淆 private static ILog log = LogM ...

  8. Python编程时.py与.pyc文件的介绍

    Python的程序中,是把原始程序代码放在.py文件里,而Python会在执行.py文件的时候.将.py形式的程序编译成中间式文件(byte-compiled)的.pyc文件,这么做的目的就是为了加快 ...

  9. pandas - 案例(美国2012年总统候选人政治献金数据分析)

    # 提供数据 months = {'JAN' : 1, 'FEB' : 2, 'MAR' : 3, 'APR' : 4, 'MAY' : 5, 'JUN' : 6, 'JUL' : 7, 'AUG' ...

  10. 微信公众号:1-IDHTTP控件:GET/POST 请求获取access_token

    (图来源于方蓓?) 首先要理解公众号的流程.通过图知道,我们要:1.你要有个web服务器,用于和微信服务器通讯.你的web服务器必须让微信服务器能找到.2.通信要求按照微信公众号开发要求的格式提供相关 ...