poj1985 Cow Marathon (求树的直径)
Cow Marathon
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 3195 | Accepted: 1596 | |
| Case Time Limit: 1000MS | ||
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
Source
#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 (求树的直径)的更多相关文章
- [POJ1985] Cow Marathon 「树的直径」
>传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...
- POJ1985 Cow Marathon (树的直径)
用两次dfs求出树的直径,这两次dfs可以写在一起,当然为了方便理解,这里是分开写的. 1 //两次dfs求树的重心 2 #include<cstdio> 3 #include<cs ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- 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找到最长路的终点, ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次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 ...
随机推荐
- oracle之sequence详解
Oracle提供了sequence对象,由系统提供自增长的序列号,每次取的时候它会自动增加,通常用于生成数据库数据记录的自增长主键或序号的地方. sequence的创建需要用户具有create seq ...
- Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)
B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...
- python用迭代器方式便利目录下的文件
from pathlib import Path pths = [pth for pth in Path.cwd().iterdir()] 如果是os.listdir()这会返回一个list,文件非常 ...
- javascript type操作
var a; type a;//结果为undefined type b;//结果也是undefinedalert(a);//弹出undefinedalert(b);//错误代码 上述代码,type ...
- Error 2103 “Unhandled Error in Silverlight Application“ 解决办法
当调试SilverLight项目时,如果出现如下错误: 当调试页面时出现如下错误: 解决办法为:打开工程属性,在Startup object:处选择相应的启动应用程序.
- 简约的ASP.NET 系统框架,提供源码。
好东西,好东西就要分享. 其实谈不上什么好东西,但很实用,应用起来也很简单,我自己用它构建了多套ASP .NET系统,原理也很简单: 1.用XML文件编辑系统的菜单和Toolbar: 2.读取XML节 ...
- DAO跨事物调用---转账
第一步创建实体类:Entity package com.beiwo.epet.entity; public class Account { private int id; private String ...
- Javaweb命名规则
一.类命名命名规范:以大写字母开头,如果有多个单词,每个单词头字母大写.例如:StudentInfo 二.接口命名命名规范:以大写字母"I"开头,如果有多个单词,每个单词头字母大写 ...
- Swagger-API测试工具实战
初次通过swagger不知道这是一个什么东东. 一.拿到一个项目需要测试,打开地址一看有个大写的swagger,于是各种脑补: 我所理解的就是,swagger是一个API开发工具或者来说是一个框架,开 ...
- xml 配置文件规范 校验
背景:做的数据同步框架,数据同步种类通过xml配置文件添加.为了系统的稳定性,我们只能认为将来写这个运行配置xml的人是一个傻瓜,那么对xml格式校验就很重要. 通过dom4j,是可以完成对xml格式 ...