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. day3 字典,集合,文件

    一.深浅copy 浅copy只copy第一层,不copy第二层.copy后,第一层指向不同内存地址.第二层指向相同的内存地址. 导入copy模块,deepcopy深copy.deepcopy后,均指向 ...

  2. Interview

    下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能"以不变应万变". 回答问题的时候能联系做过项目的例子是最好的,有的问题后面我已经补充联系到项目中的对应的案例了. 1.简述 ...

  3. MVC 访问IFrame页面Session过期后跳转到登录页面

    Web端开发时,用户登录后往往会通过Session来保存用户信息,Session存放在服务器,当用户长时间不操作的时候,我们会希望服务器保存的Session过期,这个时候,因为Session中的用户信 ...

  4. ASCIITable: 演示 Arduino 串口输出的进阶功能

    原文地址 - https://www.arduino.cc/en/Tutorial/ASCIITable ASCII字符表 本例展示了高级的串口打印功能,通过本功能可以在Arduino软件(IDE)的 ...

  5. SQL2008 清除日志

    USE [master] GO ALTER DATABASE BizTest SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE BizTest SE ...

  6. HUD 4007 [扫描线][序]

    /* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...

  7. 转换实例存储支持为EBS支持的AMI

    转换实例存储支持为EBS支持的AMI 注:不能将实例存储支持的Windows AMI 转换为 EBS 支持的 AMI.并且,你只能转换你所拥有的 AMI. 1. 从一个EBS支持的AMI启动一个Ama ...

  8. UNIX历史和标准

    1969年,bell实验室 Ken Thompson在小型机上首次实现了UNIX系统 1979年,加州伯克利分校发布了UNIX发布版--BSD, 随着AT&T不在对电信市场形成垄断,该公司被允 ...

  9. vim入门过程

    先下载了一本VIM的用户手册. 看到可以使用vimtutor(VIM的入门教程,很不错,由浅入深)作为入门. Unix系统中,请在命令行输入:vimtutor,进入教程. MS-Windows系统中, ...

  10. a链接事件点击函数

    <script> <!-- function ChangeStarCommunity(o){ var o = document.getElementById(o); if( docu ...