题解报告:poj 1985 Cow Marathon(求树的直径)
Description
Input
Output
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(求树的直径)的更多相关文章
- POJ 1985 Cow Marathon【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- [POJ1985] Cow Marathon 「树的直径」
>传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
随机推荐
- Andriod Atom x86模拟器启动报错。
用Inter Atom模式的Android模拟器启动报一下错误: Starting emulator for AVD 'new' emulator: ERROR: x86 emulation curr ...
- Django 之 中间件
一.概念 1.什么是中间件? 官方解释:中间件是用来处理Django的请求和响应的框架级别的钩子.基于全局范围产生影响. 平民解释:中间件是帮助我们在视图函数执行前和执行后做的操作.它本质上就是一个自 ...
- Java中需要了解的点
1.32位jvm.64位区别? 2.
- windows server2012之部署HTTPS安全站点
现在的互联网越来越重视网络安全方面的内容,像我们日常生活中浏览的网上银行网站等涉及安全的你都会发现有https 的标志出现,在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间 ...
- FPU同步(翻译)
本篇翻译的原英文在:http://mauve.mizuumi.net/2013/06/16/desyncs-and-fpu-synchronization/#more-725(可能要FQ) 如果你曾经 ...
- 使用 Word 2013 维护博客
博客的发布.修改是一件非常耗时.耗精力的事情.借助 Word 2013,维护博客将变得非常简单. 1 新建博客文章 运行 Word 2013,新建文档.如下图所示: 图1 鼠标左键单击上图的" ...
- 「NetworkOnMainThreadException」:Android 2.3到Android 4.0上传文件的问题
我在百度知道上的提问: 『之前我用的一段文件上传的代码,配合服务器端的servlet,实现了上传手机上的文件的功能:但是后来我把手机的Android系统从2.3升级到了4.0 ,同样的代码,同样的配置 ...
- I.MX6 MAC Address hacking
/************************************************************************** * I.MX6 MAC Address hack ...
- Ordered Fractions
链接 分析:遍历一下,求个gcd即可,最后按照ans排序并去重 /* PROB:frac1 ID:wanghan LANG:C++ */ #include "iostream" # ...
- Python Web开发最难懂的WSGI协议,到底包含哪些内容?
原文出处: PythonScientists 我想大部分Python开发者最先接触到的方向是WEB方向(因为总是有开发者希望马上给自己做个博客出来,例如我),既然是WEB,免不了接触到一些WEB框架, ...