POJ 1985 Cow Marathon (求树的直径)
Description
Input
Output
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
- #include<cstdio>
- #include<queue>
- #include<string.h>
- #define M 100000
- using namespace std;
- int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
- struct stu
- {
- int from,to,val,next;
- }st[M];
- void init()
- {
- num=;
- memset(head,-,sizeof(head));
- }
- void add_edge(int u,int v,int w)
- {
- st[num].from=u;
- st[num].to=v;
- st[num].val=w;
- st[num].next=head[u];
- head[u]=num++;
- }
- void bfs(int fir)
- {
- ans=;
- int u;
- memset(sum,,sizeof(sum));
- memset(flag,,sizeof(flag));
- queue<int>que;
- que.push(fir);
- flag[fir]=;
- while(!que.empty())
- {
- u=que.front();
- que.pop();
- for(i = head[u] ; i != - ; i=st[i].next)
- {
- if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
- {
- sum[st[i].to]=sum[u]+st[i].val;
- if(ans < sum[st[i].to])
- {
- ans=sum[st[i].to];
- node=st[i].to;
- }
- flag[st[i].to]=;
- que.push(st[i].to);
- }
- }
- }
- }
- int main()
- {
- char d;
- while(scanf("%d %d",&n,&m)!=EOF)
- {
- init();
- for(i = ; i < m ; i++)
- {
- scanf("%d %d %d %c",&a,&b,&c,&d);
- add_edge(a,b,c);
- add_edge(b,a,c);
- }
- bfs();
- bfs(node);
- printf("%d\n",ans);
- }
- }
POJ 1985 Cow Marathon (求树的直径)的更多相关文章
- POJ 1985 Cow Marathon【树的直径】
题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- [POJ1985] Cow Marathon 「树的直径」
>传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
随机推荐
- Linux下tcp服务器创建的步骤
创建一个socket,使用函数socket() socket(套接字)实质上提供了进程通信的端点,进程通信之前,双方首先必须建立各自的一个端点,否则没有办法通信.通过socket将IP地址和端口绑定之 ...
- Java | 基础归纳 | java时间格式处理总结
https://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html https://blog.csdn.net/xsj_blog/a ...
- Solutions to an Equation LightOJ - 1306
Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...
- GDI双缓冲绘图
一.简介 在进行复杂图形绘制时,若直接在屏幕DC上进行绘制,则会出现明显的闪烁.闪烁产生的原因是当绘制的图形较为 复杂时,图形绘制过程中就被刷新到屏幕上,导致结果断断续续地显示出来.双缓冲绘图的原理是 ...
- 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...
- HAL之EXIT
在STM32cubeMX中 1 在GPIO管脚上选定EXIT功能 2 在GPIO模式中设定触发边沿类型 3 在NVIC中设定NVIC分组及使能EIXT_Line0_interrupt 在MDK中的GP ...
- [已读]跨终端web
13年去听阿里技术嘉年华,鬼道分享了<移动优先前端产品的探索>.今年我买这本书,事实上是被高大上的目录吸引→ → 买来后发现,嘿,似曾相识啊,但还是老老实实得花一下午把书翻了一遍.翻完之后 ...
- mysql安装及基本概念
1.mysql下载安装 在官网下载5.6版本(越老稳定性越好,现在公司一般都用5.6),选择windows,64bit .下载完解压看bin目录下是否有mysql·exe和mysqld.exe. 解压 ...
- 【学习笔记】深入理解js原型和闭包(8)——简述【执行上下文】上
什么是“执行上下文”(也叫做“执行上下文环境”)?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时, ...
- Android studio 时间选择器
相当简单加载 gradle文件然后做一个textview即可. 1.首先我们要在build.gradle中写上这一行代码: compile 'com.feezu.liuli:timeselector: ...