Cow Marathon

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 3195   Accepted: 1596
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

 
求树的直径:两遍bfs。
1.随便找一个点进行第一遍bfs,可以保证最远的点一定是树的直径的一个端点,
2.以第一遍bfs找到的树的直径的端点为起点,进行第二遍bfs,找到另一个端点,它们之间的距离就是树的直径。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = ;
int n,m;
int head[N];
struct node {
int v,w,next;
}edge[N<<];
int cnt;
void add(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int vis[N];
int dist[N];
int que[N];
int ret;
int bfs(int u) {
memset(vis, , sizeof(vis));
int start = ;
int rear = ;
que[] = u;
vis[u] = ;
dist[u] = ;
int i;
int ans = ;
while (start < rear) {
start++;
int tmp = que[start];
for (i = head[tmp]; i != -; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v]) {
rear++;
que[rear] = v;
vis[v] = ;
dist[v] = dist[tmp] + edge[i].w;
if (dist[v] > ans) {
ans = dist[v];
ret = v;
}
}
}
}
return ans;
}
int main() {
// freopen("in.txt","r",stdin);
int i;
int u,v,w;
char ch;
while (scanf("%d %d",&n,&m) != EOF) {
memset(head, -, sizeof(head));
cnt = ;
for (i = ; i < m; i++) {
scanf("%d %d %d %c",&u,&v,&w,&ch);
add(u,v,w);
add(v,u,w);
}
bfs();
printf("%d\n",bfs(ret));
}
return ;
}

poj1985 Cow Marathon (求树的直径)的更多相关文章

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

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

  2. POJ1985 Cow Marathon (树的直径)

    用两次dfs求出树的直径,这两次dfs可以写在一起,当然为了方便理解,这里是分开写的. 1 //两次dfs求树的重心 2 #include<cstdio> 3 #include<cs ...

  3. Cow Marathon(树的直径)

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

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

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

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

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

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

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

  7. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

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

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

  9. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

随机推荐

  1. JSTL 核心标签库 使用

    JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...

  2. Flask 的 Context 机制

    转自https://blog.tonyseek.com/post/the-context-mechanism-of-flask/ Flask 的 Context 机制 2014 年 07 月 21 日 ...

  3. 升级owncloud到7.0.2

    owncloud很久没有提示升级了,官网已经升级到7.0.2,访问Update Center依旧显示是最新.估计是有什么问题导致不能自动更新.想着干脆手动升级试试.找到了两个手动升级的文档: 大版本升 ...

  4. Solr部署到Tomcat

    1.版本选择 solr-5.3.1.tgz apache-tomcat-8.0.29.tar.gz 2.解压tomcat和solr [root@iZ23exixsjaZ solr]# .tar.gz ...

  5. Mysql使用workbench迁移数据

    原文:http://jingyan.baidu.com/article/925f8cb8f3ec25c0dce05644.html 打开Mysql WorkBench,连接到数据库: 首先选中:Man ...

  6. 在Excel VBA中将SQL查询的结果赋值给变量的方法

    直接上代码示例: nowdate为日期型变量 strSql = "select DISTINCT 日期 from new_ubi_data ORDER BY 日期 DESC Limit 0, ...

  7. sql2008r 收缩数据库日志log文件;删除errorlog文件的方法

    1.清空log文件,以减少数据库文件log所占的空间 USE dbname1 ; GO ALTER DATABASE dbname1 SET RECOVERY SIMPLE;--设置简单恢复模式 GO ...

  8. extentreports报告插件与testng集成(一)

    前段时间在群里有人说了下用这个插件来生成测试报告,发现生成的报告非常不错.就下来学习了一下,并集成到了testng上,下面来分享一下: ExtentReports (by Anshoo Arora) ...

  9. 如何创建vss2005的数据库

    配置如下 VSS手工创建数据库的步骤(设数据库根目录为D:\VSS): 1.在根目录中创建名为srcsafe.ini(全局配置文件)的文件,文件内容如下: Data_Path = data Temp_ ...

  10. WIN7-修改域名

    前言:访问本地项目有如下的几种方式,其中通过修改域名配置文件的方式使用的较少,不过在某些项目中也会使用的到(比如:我曾经参与的一个项目使用到SpringCloud框架,需要将每个微服务注册到,服务控制 ...