题目大意:给出一棵树。求两点间的最长距离。

思路:裸地树的直径。两次BFS,第一次随便找一个点宽搜。然后用上次宽搜时最远的点在宽搜。得到的最长距离就是树的直径。

CODE:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 80010
using namespace std; int points,edges;
int head[MAX],total;
int next[MAX],aim[MAX],length[MAX]; int f[MAX]; char s[10]; inline void Add(int x,int y,int len);
inline void BFS(int start); int main()
{
cin >> points >> edges;
for(int x,y,len,i = 1;i <= edges; ++i) {
scanf("%d%d%d%s",&x,&y,&len,s);
Add(x,y,len),Add(y,x,len);
}
BFS(1);
int _max = 0;
for(int i = 1;i <= points; ++i)
if(f[i] > f[_max])
_max = i;
BFS(_max);
int ans = 0;
for(int i = 1;i <= points; ++i)
if(f[i] > ans)
ans = f[i];
cout << ans << endl;
return 0;
} inline void Add(int x,int y,int len)
{
next[++total] = head[x];
aim[total] = y;
length[total] = len;
head[x] = total;
} inline void BFS(int start)
{
static queue<int> q;
while(!q.empty()) q.pop();
memset(f,0x3f,sizeof(f));
f[0] = f[start] = 0;
q.push(start);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = head[x];i;i = next[i])
if(f[aim[i]] > f[x] + length[i]) {
f[aim[i]] = f[x] + length[i];
q.push(aim[i]);
}
}
}

BZOJ 3363 POJ 1985 Cow 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. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

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

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

  4. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

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

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

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

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

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

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

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

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  9. POJ 1985 Cow Marathon (树形DP,树的直径)

    题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...

随机推荐

  1. Python常见文件操作的函数示例

    # -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...

  2. [转]automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)

    @当我们在一个UIViewController中同时创建2个tableView的时候,如果把它们的frame中的Y坐标设置为一样,你可能会发现它们的位置并没有达到你想要的结果.比如第一tableVie ...

  3. Django中的csrf相关装饰器

    切记:  这俩个装饰器不能直接加在类中函数的上方 (CBV方式) csrf_exempt除了,csrf_protect受保护的   from django.views import Viewfrom ...

  4. ython——杂货铺

    三目运算: >>> 1 if 5>3 else 0 1 >>> 1 if 5<3 else 0 0 深浅拷贝: 一.数字和字符串 对于 数字 和 字符串 ...

  5. js 获取json对象的Key、value

    <script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...

  6. dp的两个不错的题

    C - Cheapest Palindrome Keeping track of all the cows can be a tricky task so Farmer John has instal ...

  7. spring配置druid连接池和监控数据库访问性能

    Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...

  8. Git 撤消操作

    修改最后一次提交 有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了.想要撤消刚才的提交操作,可以使用 --amend 选项重新提交: $ git commit --amend 此命令将 ...

  9. 【kmp+最小循环节】poj 2406 Power Strings

    http://poj.org/problem?id=2406 [题意] 给定字符串s,s=a^n,a是s的子串,求n最大是多少 [思路] kmp中的next数组求最小循环节的应用 例如 ababab ...

  10. 「CodePlus 2018 3 月赛」白金元首与莫斯科

    $n \leq 17,m \leq 17$,$n*m$的01矩形,对每一个0问:当他单独变成1之后,在其他0处放多米诺牌(不一定放满,可以不放)的方案数.膜$1e9+7$. 直接$dp$是$n^42^ ...