poj 2631 Roads in the North(裸树的直径)
2015-08-01 22:02
330 查看
Description
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
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
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
Sample Output
#include<iostream>
#include<stdio.h>
#include <string.h>
#include<algorithm>
using namespace
std;
#define maxe 20010
struct node
{
int v,w;
int next;
}Edge[maxe];
int head[maxe];
int len;
bool vis[maxe];
void add(int a,int b,int w)
{
Edge[len].v=b;
Edge[len].w=w;
Edge[len].next=head[a];
head[a]=len++;
}
int ans;
int temp;
void dfs(int u,int value)
{
if(ans<value)
{
ans=value;
temp=u;
}
vis[u]=true;
for (int i=head[u]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].v;
if(!vis[v])
{
dfs(v, value+Edge[i].w);
}
}
}
int main()
{
int u,v,w;
len=0;
memset(head, -1,sizeof(head));
while(scanf("%d%d%d",&u,&v,&w)!=EOF)
{
add(u,v,w);
add(v,u,w);
}
ans=0;
memset(vis,false,
sizeof(vis));
dfs(1,0);
ans=0;
memset(vis,false,
sizeof(vis));
dfs(temp,0);
printf("%d\n",ans);
return 0;
}
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
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
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 6 1 4 5 6 3 9 2 6 8 6 1 7
Sample Output
22
#include<iostream>
#include<stdio.h>
#include <string.h>
#include<algorithm>
using namespace
std;
#define maxe 20010
struct node
{
int v,w;
int next;
}Edge[maxe];
int head[maxe];
int len;
bool vis[maxe];
void add(int a,int b,int w)
{
Edge[len].v=b;
Edge[len].w=w;
Edge[len].next=head[a];
head[a]=len++;
}
int ans;
int temp;
void dfs(int u,int value)
{
if(ans<value)
{
ans=value;
temp=u;
}
vis[u]=true;
for (int i=head[u]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].v;
if(!vis[v])
{
dfs(v, value+Edge[i].w);
}
}
}
int main()
{
int u,v,w;
len=0;
memset(head, -1,sizeof(head));
while(scanf("%d%d%d",&u,&v,&w)!=EOF)
{
add(u,v,w);
add(v,u,w);
}
ans=0;
memset(vis,false,
sizeof(vis));
dfs(1,0);
ans=0;
memset(vis,false,
sizeof(vis));
dfs(temp,0);
printf("%d\n",ans);
return 0;
}
相关文章推荐
- 将UE添加到右键菜单
- 高性能javascript读书笔记(二.数据访问)
- 说反话(c++实现)
- linux下WMB通过ODBC连接DB2数据库
- wxPython中菜单、按钮学习
- 说反话(c++实现)
- 说反话(c++实现)
- Swift入门(五)——数组(Array)
- [leedcode 153] Find Minimum in Rotated Sorted Array
- 说反话(c++实现)
- 大数据时代让oracle跑的更快
- c/c++ int _tmain(int argc, _TCHAR* argv[])
- C函数之memcpy()函数用法
- 说反话(c++实现)
- MySQL 中的事务概念详解
- 说反话(c++实现)
- BZOJ 3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛( dp )
- PyQt5 Introduction and components
- httpclient中文API
- 说反话(c++实现)