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

  1. 7 6
  2. 1 6 13 E
  3. 6 3 9 E
  4. 3 5 7 S
  5. 4 1 3 N
  6. 2 4 20 W
  7. 4 7 2 S

Sample Output

  1. 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. 
 
第一行输入n,m表示n个节点,m种关系,然后m行输入三个数a,b,c,表示a,b节点间的距离是c,求节点间最远的距离。S,N,W不影响结果,只在开始时输入就行。
  1. #include<cstdio>
  2. #include<queue>
  3. #include<string.h>
  4. #define M 100000
  5. using namespace std;
  6. int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
  7. struct stu
  8. {
  9. int from,to,val,next;
  10. }st[M];
  11. void init()
  12. {
  13. num=;
  14. memset(head,-,sizeof(head));
  15. }
  16. void add_edge(int u,int v,int w)
  17. {
  18. st[num].from=u;
  19. st[num].to=v;
  20. st[num].val=w;
  21. st[num].next=head[u];
  22. head[u]=num++;
  23. }
  24. void bfs(int fir)
  25. {
  26. ans=;
  27. int u;
  28. memset(sum,,sizeof(sum));
  29. memset(flag,,sizeof(flag));
  30. queue<int>que;
  31. que.push(fir);
  32. flag[fir]=;
  33. while(!que.empty())
  34. {
  35.  
  36. u=que.front();
  37. que.pop();
  38. for(i = head[u] ; i != - ; i=st[i].next)
  39. {
  40. if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
  41. {
  42. sum[st[i].to]=sum[u]+st[i].val;
  43. if(ans < sum[st[i].to])
  44. {
  45. ans=sum[st[i].to];
  46. node=st[i].to;
  47. }
  48. flag[st[i].to]=;
  49. que.push(st[i].to);
  50. }
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. char d;
  57. while(scanf("%d %d",&n,&m)!=EOF)
  58. {
  59.  
  60. init();
  61. for(i = ; i < m ; i++)
  62. {
  63. scanf("%d %d %d %c",&a,&b,&c,&d);
  64. add_edge(a,b,c);
  65. add_edge(b,a,c);
  66. }
  67. bfs();
  68. bfs(node);
  69. printf("%d\n",ans);
  70. }
  71. }

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

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

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

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

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

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

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

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

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

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

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

  6. poj 1985 Cow Marathon

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

  7. Cow Marathon(树的直径)

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

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

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

  9. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

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

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

随机推荐

  1. Linux下tcp服务器创建的步骤

    创建一个socket,使用函数socket() socket(套接字)实质上提供了进程通信的端点,进程通信之前,双方首先必须建立各自的一个端点,否则没有办法通信.通过socket将IP地址和端口绑定之 ...

  2. Java | 基础归纳 | java时间格式处理总结

    https://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html https://blog.csdn.net/xsj_blog/a ...

  3. Solutions to an Equation LightOJ - 1306

    Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...

  4. GDI双缓冲绘图

    一.简介 在进行复杂图形绘制时,若直接在屏幕DC上进行绘制,则会出现明显的闪烁.闪烁产生的原因是当绘制的图形较为 复杂时,图形绘制过程中就被刷新到屏幕上,导致结果断断续续地显示出来.双缓冲绘图的原理是 ...

  5. 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值

    假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...

  6. HAL之EXIT

    在STM32cubeMX中 1 在GPIO管脚上选定EXIT功能 2 在GPIO模式中设定触发边沿类型 3 在NVIC中设定NVIC分组及使能EIXT_Line0_interrupt 在MDK中的GP ...

  7. [已读]跨终端web

    13年去听阿里技术嘉年华,鬼道分享了<移动优先前端产品的探索>.今年我买这本书,事实上是被高大上的目录吸引→ → 买来后发现,嘿,似曾相识啊,但还是老老实实得花一下午把书翻了一遍.翻完之后 ...

  8. mysql安装及基本概念

    1.mysql下载安装 在官网下载5.6版本(越老稳定性越好,现在公司一般都用5.6),选择windows,64bit .下载完解压看bin目录下是否有mysql·exe和mysqld.exe. 解压 ...

  9. 【学习笔记】深入理解js原型和闭包(8)——简述【执行上下文】上

    什么是“执行上下文”(也叫做“执行上下文环境”)?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时, ...

  10. Android studio 时间选择器

    相当简单加载 gradle文件然后做一个textview即可. 1.首先我们要在build.gradle中写上这一行代码: compile 'com.feezu.liuli:timeselector: ...