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
解题思路:求树的直径,多了边权,改一下模板即可。
AC代码(102ms):两次bfs。
 #include<iostream>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=1e6+;
struct EDGE{int to,cap,next;}edge[maxn<<];
struct node{
int u,tot;
node(int x,int y):u(x),tot(y){}
};
int n,x,y,w,q,cnt,res,maxcap,maxvex,head[maxn];char ch;bool vis[maxn];
queue<node> que;
void add_edge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u,int cap,int &maxcap,int &maxvex){
while(!que.empty())que.pop();
memset(vis,false,sizeof(vis));
que.push(node(u,cap));vis[u]=true;
while(!que.empty()){
node nod=que.front();que.pop();
for(int i=head[nod.u];~i;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
vis[v]=true;
que.push(node(v,nod.tot+edge[i].cap));//先把其邻接点都入队,并标记为已访问
}
}
if(nod.tot>maxcap)maxcap=nod.tot,maxvex=nod.u;//再更新当前节点的深度
}
}
int main(){
while(~scanf("%d%d",&n,&q)){
memset(head,-,sizeof(head));cnt=;
while(q--){
scanf("%d %d %d %c",&x,&y,&w,&ch);
add_edge(x,y,w);
add_edge(y,x,w);
}
maxcap=,maxvex=;
bfs(,,maxcap,maxvex);maxcap=;
bfs(maxvex,,maxcap,maxvex);
printf("%d\n",maxcap);
}
return ;
}
AC代码二(157ms):一次dfs。
 #include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=1e6+;
struct EDGE{int to,cap,next;}edge[maxn<<];
int n,x,y,w,q,cnt,res,maxdist,head[maxn];char ch;
void add_edge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u,int fa,int &maxdist){
int Dmax=,Dsec=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
int nowd=dfs(v,u,maxdist)+edge[i].cap;
if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
else if(nowd>Dsec)Dsec=nowd;
}
}
maxdist=max(maxdist,Dmax+Dsec);
return Dmax;
}
int main(){
while(~scanf("%d%d",&n,&q)){
memset(head,-,sizeof(head));cnt=;
while(q--){
scanf("%d %d %d %c",&x,&y,&w,&ch);
add_edge(x,y,w);
add_edge(y,x,w);
}
maxdist=;
dfs(,-,maxdist);
printf("%d\n",maxdist);
}
return ;
}
 

题解报告:poj 1985 Cow Marathon(求树的直径)的更多相关文章

  1. POJ 1985 Cow Marathon【树的直径】

    题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...

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

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

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

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

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

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

  5. poj 1985 Cow Marathon

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

  6. Cow Marathon(树的直径)

    传送门 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5362   Accepted: 2634 ...

  7. [POJ1985] Cow Marathon 「树的直径」

    >传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...

  8. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

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

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

随机推荐

  1. VC断点失败的原因之中的一个

    VC断点失败的原因之中的一个 flyfish 2014-10-23 情景 再debug状态下仅仅有一个cpp文件.命中不了断点. 提示 能够 同意源码与原始版本号不同 不採用,防止出现未知的隐患 问题 ...

  2. mysql创建 存储过程 并通过java程序调用该存储过程

    create table users_ning(id primary key auto_increment,pwd int); insert into users_ning values(id,123 ...

  3. linux设备驱动学习笔记(1)

    学习了将近半个月的设备驱动程序的编写,也有一些体会,这里写下来也给学习做一个总结,为后面的学习做更好的准备. 首先,个人感觉驱动程序的设计是很有套路的,最基本的要求就是要掌握这些套路.所谓的套路就是一 ...

  4. vue中如何实现后台管理系统的权限控制

    vuejs单页应用的权限管理实践 一.前言 在广告机项目中,角色的权限管理是卡了挺久的一个难点.首先我们确定的权限控制分为两大部分,其中根据粒的大小分的更细: 接口访问的权限控制 页面的权限控制 菜单 ...

  5. Hihocoder #1077 : RMQ问题再临-线段树(线段树:结构体建树+更新叶子往上+查询+巧妙使用father[]+线段树数组要开大4倍 *【模板】)

    #1077 : RMQ问题再临-线段树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到:小Hi给小Ho出了这样一道问题:假设整个货架上从左到右摆放了N种商品,并 ...

  6. HDU3338 Kakuro Extension —— 最大流、方格填数类似数独

    题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)     ...

  7. POJ1077 Eight —— 经典的搜索问题

    题目链接:http://poj.org/problem?id=1077 Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  8. DataUtils

    package com.cc.hkjc.util; import java.text.ParseException;import java.text.SimpleDateFormat;import j ...

  9. Opencv实现两幅图像融合

    实现两幅图像线性(不同系数下)的融合涉及到Opencv中两个关键的方法,addWeighted()和createTrackbar() addWeighted方法: 函数原型: void addWeig ...

  10. I.MX6 Android 5.1.1 下载、编译

    /************************************************************************* * I.MX6 Android 5.1.1 下载. ...