博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj--1985--Cow Marathon(树的直径)
阅读量:5216 次
发布时间:2019-06-14

本文共 1880 字,大约阅读时间需要 6 分钟。

Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4424   Accepted: 2214
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 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 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找到树的直径

#include
#include
#include
#include
using namespace std;#define MAXN 50100int head[MAXN],dis[MAXN],vis[MAXN];int m,n,cnt,Node,ans;struct node{ int u,v,val; int next;}edge[MAXN*10];void init(){ memset(head,-1,sizeof(head)); cnt=0;}void add(int u,int v,int val){ node E={u,v,val,head[u]}; edge[cnt]=E; head[u]=cnt++;}void getmap(){ char op[2]; int a,b,c; for(int i=0;i
q; memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); ans=0; Node=sx; q.push(Node); vis[Node]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { node E=edge[i]; if(!vis[E.v]) { vis[E.v]=1; dis[E.v]=dis[E.u]+E.val; q.push(E.v); } } } for(int i=1;i<=n;i++) { if(ans

转载于:https://www.cnblogs.com/playboy307/p/5273525.html

你可能感兴趣的文章
【 js 基础 】【读书笔记】Javascript “继承”
查看>>
五.Hystrix请求缓存(request cache)
查看>>
Python+OpenCV图像处理之图像金字塔
查看>>
你的日志组件记录够清晰嘛?--自己开发日志组件 Logger
查看>>
简版的文件传输
查看>>
input(Text)控件作为填空输入,但运行后,有曾经输入的记录显示,用autocomplete="off"解决...
查看>>
Java多线程
查看>>
长春理工大学第十四届程序设计竞赛(重现赛)J
查看>>
统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数...
查看>>
leetcode36 有效数独
查看>>
jQuery选择器和遍历的总结
查看>>
ThreadPerMessagePattern——关于匿名内部类
查看>>
osg 3ds模型加载与操作
查看>>
[转帖]IBM收购红帽价格是多少?是否会形成垄断企业?会存在什么不安因素?...
查看>>
[转]Whirlwind Tour of ARM Assembly
查看>>
python socket.error: [Errno 10054] 解决方法
查看>>
JavaScript 高级篇之函数 (五)
查看>>
本周个人总结
查看>>
C# 中在Form控件创建以外的线程操作控件问题
查看>>
改写二分搜索算法及对于问题的理解
查看>>