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 ...
随机推荐
- iOS设备、Icon、LaunchImage、图片分辨率
iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...
- 转:Windows 8上强制Visual Studio以管理员身份运行
Windows 8的一个既安全又蛋疼之处是UAC的行为被改变了.以往在Windows 7中,只要关闭了UAC,自己的帐号又是本机管理员组的,任何程序都会以管理员身份启动.然而,在Windows 8上, ...
- Java8 map和reduce
map final List<Integer> numbers = Arrays.asList(1, 2, 3, 4); final List<Integer> doubleN ...
- C/C++中文的编码和字符串处理
windows平台 char 表示单字符,占用一个字节 wchar_t 表示宽字符,占用两个字节 Linux平台 char 占用一个字节 wchar_t 占用四个字节 windows平台下对于用字符串 ...
- Sdut 2151 Phone Numbers (山东省ACM第一届省赛题 A)
题目描述 We know thatif a phone number A is another phone number B's prefix, B is not able to becalled. ...
- 桂电在线-php-提取菜单到配置文件
新建存储菜单的配置文件 menus.php,并配置菜单 <?php if ( ! defined('BASEPATH')) exit('No direct script access allow ...
- "The connection for the USB device '###' was unsuccessful. The device is currently in use"
一.问题描述 1.情景描述 笔者的物理主机系统是“windows7 64位”,想使用“摄像头录像大师”.这个软件在录制视频的过程中,需要调用windows自带的"windows media ...
- JMX示例
HelloJMXMBean.java package jmx; /** * Created by george on 14-8-21. */ public interface HelloJMXMBea ...
- App接口设计原则-b
1.记住密码不是真的让你记住密码,这里仅仅指的是一种自动登录的手段.不管在任何地方,明文存储的密码都是安全隐患,是必须尽量避免的.你可以采用某种方式对用户名.密码以及时间戳(重要)进行签名,再次登录时 ...
- JDK下sun.net.www.protocol.http.HttpURLConnection类-----Http客户端实现类的实现分析
HttpClient类是进行TCP连接的实现类, package sun.net.www.http; import java.io.*; import java.net.*; import java. ...