此题求的是树的最长链, 要注意此题不能用dis[maxn][maxn]来记录a, b两点的距离,这样会超空间, 也就是超内存,要用另一种储存方式储存。此题思想很简单, 随便选一个点,搜此点能到的最远距离, 并记录最远距离的点, 然后再从最远距离的点搜这点能到的最远距离, 这个dis就是答案。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;int m, n, ans, flag, num, head[100005];
bool vis[100005];struct eage
{int to;int next;int eg;
}e[100005];void add(int a, int b, int eg)
{e[num].to = b;e[num].eg = eg;e[num].next = head[a];head[a] = num++;e[num].to = a;e[num].eg = eg;e[num].next = head[b];head[b] = num++;
}void bfs(int a)
{int dis = 0, he, to;queue<int> q;q.push(a);q.push(dis);while(!q.empty()){he = q.front();vis[he] = 1;q.pop();dis = q.front();q.pop();to = head[he];while(vis[e[to].to] == 1&&to != -1){to = e[to].next;}while(to != -1&&vis[e[to].to] == 0){q.push(e[to].to);q.push(dis + e[to].eg);if(ans < (dis + e[to].eg))ans = dis + e[to].eg, flag = e[to].to;to = e[to].next;while(vis[e[to].to] == 1&&to != -1){to = e[to].next;}}}
}int main(int argc, char *argv[])
{int i, j, k, l;ans = 0;num = 0;mem(e);mem(vis);memset(head, -1, sizeof(head));scanf("%d%d",&m, &n);for(i = 0;i < n;i++){scanf("%d%d%d%*c%*c%*c", &j, &k, &l);add(j, k, l);}bfs(1);mem(vis);bfs(flag);printf("%d\n", ans);return 0;
}

POJ 1985 Cow Marathon相关推荐

  1. poj 1985 Cow Marathon 【树的直径】

    求树的直径 /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostre ...

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

    给出一棵树 ,和边的权值 求权值最长的一条直径 两次bfs求 第一次以任意点开始 BFS求出第一个端点 第二次以第一次得到的端点 BFS求出第二个端点 #include <cstdio> ...

  3. POJ 3660 Cow Contest [Floyd]

    POJ - 3660 Cow Contest http://poj.org/problem?id=3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1. ...

  4. poj1985 Cow Marathon(树的直径#入门)

    poj1985 Cow Marathon(树的直径) Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 9110 Accepted: ...

  5. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. [POJ 3270]Cow Sorting

    Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...

  7. poj 3045 Cow Acrobats (贪心!!不是二分,)

    农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 <= W_i <= 10,000)和力量( ...

  8. POJ 3263-Tallest Cow

    Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow ...

  9. POJ 3660 Cow Contest

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

最新文章

  1. 呼之欲出的量子计算机和漫长的最后一公里
  2. gometalinter代码检查
  3. 请说明一下http和https的区别
  4. 其它综合-CentOS7 忘记root密码
  5. 关于提高网页加载速度个人学习以及经验总结
  6. 学java要背的单词_学习JAVA必背的8类常用单词
  7. 【新书速递】你想知道的通信“灵魂三问”都在这里……(福利再现)
  8. Python函数合集:68个内置函数请收好!
  9. springsecurity 认证之密码模式
  10. CentOS下双网卡绑定-bond0
  11. Mysql常用内置函数
  12. 数学分析思维导图整理(四)单变量积分学
  13. 后端日志【22】:时间过的很快,坚持的第7.3个月过去了,我有什么变化?
  14. 油猴脚本对web项目的影响
  15. 个人博客建站中jekyll/hexo/Hugo各自的优势与区别
  16. 看电脑头痛计算机专业,看电脑头疼怎么回事
  17. Win10 点击任务栏固定的文件夹资源管理器就重启
  18. 使用hexo基于cactus仙人掌主题最全美化客制教程
  19. 一个压长定式衍生出的一个角部攻防
  20. 概率论笔记—一维随机变量及其分布

热门文章

  1. MySql 数据库查看视图的创建sql语句
  2. vs19c++求arccos值
  3. Densely Connected Convolutional Networks(文献阅读笔记)
  4. 编写一个模拟航空客运定票系统的程序
  5. Python 高级编程(第2版)
  6. FL Studio下载注册码激活序列号及2020新手入门基础使用教程
  7. 北京:楼盘内部认购禁而不止 业内揭秘花招多
  8. 近750亿到位 联通混改战略投资者已完成现金认购
  9. Cdiscount的订单管理ERP怎么选择?无忧易售ERP已与Cdiscount达成合作,完成对接
  10. 计算机硬件选型报价,计算机系统硬件选型