Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 5117   Accepted: 2492
Case Time Limit: 1000MS

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. 

Source


裸题
换一种DP写法
小心不能用f[j][1]来更新f[i],因为是从i节点的不同子树中选
//
// main.cpp
// poj1985
//
// Created by Candy on 9/21/16.
// Copyright ? 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e4+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,u,v,w,ans=; char c[];
struct edge{
int ne,v,w;
}e[N<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v; e[cnt].w=w; e[cnt].ne=h[u]; h[u]=cnt;
cnt++;
e[cnt].v=u; e[cnt].w=w; e[cnt].ne=h[v]; h[v]=cnt;
}
int f[N][];
void dp(int u,int fa){
f[u][]=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(v==fa) continue;
dp(v,u);
if(f[v][]+w>f[u][]){
f[u][]=f[u][];
f[u][]=f[v][]+w;
}else{
if(f[v][]+w>f[u][]) f[u][]=f[v][]+w;
}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=m;i++){
u=read();v=read();w=read();
ins(u,v,w);
scanf("%s",c);
}
memset(f,-,sizeof(f));
dp(,);
for(int i=;i<=n;i++) ans=max(ans,f[i][]+f[i][]);
printf("%d",ans);
return ;
}

POJ1985Cow Marathon[树的直径]的更多相关文章

  1. poj 1985 Cow Marathon 树的直径

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

  2. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

  3. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

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

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4424   Accepted: 2214 Case ...

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

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

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

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

  7. 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

    3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ​ 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...

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

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

  9. Cow Marathon(树的直径)

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

随机推荐

  1. Ext.store.load callback

    var paramsReceivable = {};                paramsReceivable.querytext = Ext.getCmp('hiddquerytext').g ...

  2. Delphi 时间耗时统计

    处理事情: 数据处理过程中,速度很慢,无法准确定位分析是DB问题还是客户端处理问题,所以增加计时统计日志: Delphi计时首次使用,查阅资料,予以记录: var BgPoint, EdPoind: ...

  3. MSCRM 报表显示 rsprocessingaborted 错误

    今天又有朋友遇到rsprocessingaborted这个问题,想想这个已经是很老很老的问题了,得在写一遍补充下. 一.首先会考虑是不是SrsDataConnector没有安装的原因,如果正常安装的话 ...

  4. 初试在线破解工具Hydra爆破3389服务器

    hydra是一款全能的暴力破解工具,功能强大,几乎支持所有的协议,是著名黑客组织thc开发的. 在Kali Linux下已经是默认安装的,于是测试爆破一下自己的一台VM虚拟机服务器.hydra还支持G ...

  5. 8月7号晚7点Autodesk北京办公室,我们来聊聊HTML5/ WebGL 3D 模型浏览技术

    Autodesk 发布了一款完全无需插件的三维模型浏览器 Autodesk 360 Viewer,大家有没有兴趣,下班后过来聊聊吧!   8月7号 周四, 19:00~21:00 Autodesk北京 ...

  6. Android Tips: 打电话和发短信

    利用Android打电话非常简单,直接调用Android内在的电话功能就可以了. btnDail.setOnClickListener(new OnClickListener(){ @Override ...

  7. CSS 遗漏点

    link属性:链接的颜色(如果改成白色什么都看不见) alink属性:active点击之后的链接的颜色 vlink属性:visited点击之后的颜色 bgcolor属性:背景颜色 word-break ...

  8. 你真的了解UIWindow吗?

    一:首先查看一下关于UIWindow的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIWindow : UIView //window的屏幕,默认是 [UISc ...

  9. 使用开源免费类库在.net中操作Excel

    自从上次找到NPOI之后,根据园友提供的线索以及Google,又找到了一些开源免费的类库,所以都简单体验了一遍. 主要找到以下类库: MyXls(http://sourceforge.net/proj ...

  10. T-SQL基础--TOP

    理解TOP子句 众所周知,TOP子句可以通过控制返回行的数量来影响查询. 我们知道TOP子句能很容易的满足返回指定行数的子集,接下来有一些例子来展示什么情况下使用TOP子句来返回一个结果集: 你打算返 ...