poj--1985--Cow Marathon(树的直径)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 4424 | Accepted: 2214 | |
Case Time Limit: 1000MS |
Description
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
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
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.
输的直径,先找到最长路径,然后以最长路径的端点开始找,第二次bfs找到树的直径
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 50100
int head[MAXN],dis[MAXN],vis[MAXN];
int m,n,cnt,Node,ans;
struct node
{
int u,v,val;
int next;
}edge[MAXN*10];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void getmap()
{
char op[2];
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%s",&a,&b,&c,op);
add(a,b,c);
add(b,a,c);
}
}
void bfs(int sx)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
ans=0;
Node=sx;
q.push(Node);
vis[Node]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[E.u]+E.val;
q.push(E.v);
}
}
}
for(int i=1;i<=n;i++)
{
if(ans<dis[i])
{
ans=dis[i];
Node=i;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
bfs(1);
bfs(Node);
printf("%d\n",ans);
}
return 0;
}
poj--1985--Cow Marathon(树的直径)的更多相关文章
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- BZOJ 3363 POJ 1985 Cow Marathon 树的直径
题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- 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 (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
随机推荐
- SQLServer int转float
例: select 2/4 会得到0 改为 select 2/4.0 则会得到0.500000 也同时达到了int转float的效果
- 剖析Promise内部结构,一步一步实现一个完整的、能通过所有Test case的Promise类
本文写给有一定Promise使用经验的人,如果你还没有使用过Promise,这篇文章可能不适合你,建议先了解Promise的使用 Promise标准解读 1.只有一个then方法,没有catch,ra ...
- Oracle数据库实例
数据库通常由两部分组成:数据库和数据库实例 数据库与实例的关系:数据库指的是:物理数据.数据库管理系统.即物理数据.内存.操作系统.用户访问Oracle都是访问一个实例,实例名指的是用于相应某个数据库 ...
- javascript中的原型对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SqlServer与MySql 系统表查询自建表数据
SqlServer: SELECT * FROM sys.sysobjects WHERE type='U' ORDER BY name SELECT * FROM sys.syscolumns WH ...
- dotnetnuke 调用第三方dll出错 System.Security.Permissions.SecurityPermission,型的权限已失败。
在dnn下调用第三方dll的微信sdk ,代码如下: WebClient wc = new WebClient(); wc.Encoding = encoding ?? Encoding.UTF8; ...
- ORACLE 11g 导出数据
ORACLE 11g 导出 表的时候 不会导出空表 导出空表操作步骤 :(使用PLSQL) 1.打开SQL window 执行下面的 SQL Select 'alter table '||table_ ...
- Javaee 方法的格式和注意事项
1.构造方法的格式是什么?有哪些注意事项? 修饰符+方法名称+(参数列表),构造的方法没有返回值,方法名称要和类名一样,有属性参数的需要在成员变量前加this,参数列表的值要和指定的方法格式相同. ...
- BZOJ 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 幼儿园测试题
本来以为是一道数学题,一顿XJBT导式子,结果就是个幼儿园都会的模拟. Code: #include<bits/stdc++.h> #define ll long long using n ...
- 【前端】CSS隐藏元素的方法和区别
CSS隐藏元素的方法和区别 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...