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. 微信小程序存放视频文件到阿里云用到算法js脚本文件

           peterhuang007/weixinFileToaliyun: 微信小程序存放视频文件到阿里云用到算法js脚本文件 https://github.com/peterhuang007/ ...

  2. spark mongo 性能优化

    性能优化事项 http://www.mongoing.com/wp-content/uploads/2016/08/MDBSH2016/TJ_MongoDB+Spark.pdf MongoDB + S ...

  3. 使用URL dispatcher的范例

    在上面的一篇文章中,我们介绍了怎样使用URL disptacher.在这篇文章中,我们来通过一个范例更进一步来了解怎样实现它. 1)创建一个具有URL dispatcher的应用 我们首先打开我们的S ...

  4. ruby 学习网站

    Ruby on Rails官网: http://rubyonrails.org/ Rails Guides:http://guides.rubyonrails.org/ -中文版: http://gu ...

  5. HDU3652 B-number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3652 B-number Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  6. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  7. 通过Oracle sql developer从sqlserver迁移数据到oracle

    通过Oracle sql developer工具从sqlserver迁移数据到oracle 序言 一般情况下,sqlserver数据迁移到oracle,我们可以使用ODI来进行.但ODI的安装.配置. ...

  8. MFC显示Mat图片

    Opencv在1.0时代,在MFC框架上显示图片可以通过Cvvimage类里的DrawPicToHDC( IplImage *img, UINT ID)方法方便的显示出来,当然当时使用的还是IpIIm ...

  9. Android中的Handler,以及用Handler延迟执行

    项目中遇到一个情况,先生成文件再上传:但有时候发出指令后上传会不成功,需要再发一次指令方能上传. 猜想是由于文件还没生成就执行「上传」指令了.想到要延时.Android中单纯用currentThrea ...

  10. Ordered Fractions

    链接 分析:遍历一下,求个gcd即可,最后按照ans排序并去重 /* PROB:frac1 ID:wanghan LANG:C++ */ #include "iostream" # ...