poj 2631 Roads in the North【树的直径裸题】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2359 | Accepted: 1157 |
Description
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Output
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22 告诉我不是我一个人看到输入数据时不知道是什么意思
树的直径裸题
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 40000
using namespace std;
int head[MAX];
int vis[MAX],dis[MAX];
int ans,sum,beg;
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void bfs(int sx)
{
int i,j;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
queue<int>q;
sum=0;
beg=sx;
q.push(sx);
while(!q.empty())
{
int top=q.front();
q.pop();
for(i=head[top];i!=-1;i=edge[i].next)
{
int x=edge[i].v;
if(!vis[x])
{ vis[x]=1;
dis[x]=dis[top]+edge[i].w;
q.push(x);
if(sum<dis[x])
{
sum=dis[x];
beg=x;
}
}
}
}
}
int main()
{
ans=0;
memset(head,-1,sizeof(head));
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
add(a,b,c);
add(b,a,c);
}
bfs(1);
bfs(beg);
printf("%d\n",sum);
return 0;
}
poj 2631 Roads in the North【树的直径裸题】的更多相关文章
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
随机推荐
- orainstRoot.sh到底执行了哪些操作
1 #!/bin/sh 1 #!/bin/sh 2 AWK=/bin/awk 3 CHMOD=/bin/chmod 4 CHGRP=/bin/chgrp ...
- MVC5学习笔记
买了一本MVC5的书:ASP.NET MVC 5 高级编程(第5版).边学边记录一下 1.快速创建模型类,如:自动实现的属性 {get;set;} 输入“prop",按Tab两次,默认属性值 ...
- 生产者与消费者(三)---BlockingQueue
前面阐述了实现生产者与消费者问题的两种方式:wait() / notify()方法 和 await() / signal()方法,本文继续阐述多线程的经典问题---生产者与消费者的第三种方式:Bloc ...
- jQuery实现商品楼层的感觉
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Kafka源码分析-序列2 -Producer
在上一篇,我们从使用方式和策略上,对消息队列做了一个宏观描述.从本篇开始,我们将深入到源码内部,仔细分析Kafka到底是如何实现一个分布式消息队列.我们的分析将从Producer端开始. 从Kafka ...
- Start an installation from GRUB
Start an installation from GRUB Description This tip will show you how to start an installation for ...
- gnuplot使用
直接用yum安装gnuplot即可,例如 sudo sh -c "yum install gnuplot.x86_64 " 安装以后就可以使用了 编写gnuplot脚本 # grp ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- SqlHelper 帮助文档及详解--项目初步搭建
微软SqlHelper类中文注释和使用方法 相关链接: http://blog.csdn.net/itmaxin/article/details/7609566 SqlHelper.cs是N年前微软出 ...
- sqlplus 打印很乱,而且很短就换行
set linesize 可以解决 设置行打印的字符长度,set linesize 400解决