首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2631 - Roads in the North(树的直径)

【POJ】2631 - Roads in the North(树的直径)

作者头像
FishWang
发布2025-08-26 15:08:30
发布2025-08-26 15:08:30
10300
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Roads in the North

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 2526

Accepted: 1241

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

代码语言:javascript
代码运行次数:0
运行
复制
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
22

Source

The UofA Local 1999.10.16

清新脱俗的邻接表 + 队列 + 两次dfs就可以啦。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 10000
int n;
vector<int> mapp[MAX+5];
vector<int> dis[MAX+5];
int f[MAX+5];		//距离当前点的距离 
bool vis[MAX+5];
int dfs(int x)
{
	int ans;		//最远点 
	int maxx = 0;		//最大距离 
	queue<int> q;		//存放根 
	CLR(vis,false);
	CLR(f,0);
	f[x] = 0;
	vis[x] = true;
	q.push(x);
	while (!q.empty())
	{
		int st = q.front();
		q.pop();
		for (int i = 0 ; i < mapp[st].size() ; i++)
		{
			if (!vis[mapp[st][i]])		//该点没有用过 
			{
				q.push(mapp[st][i]);
				vis[mapp[st][i]] = true;
				f[mapp[st][i]] = f[st] + dis[st][i];
				if (f[mapp[st][i]] >= maxx)
				{
					maxx = f[mapp[st][i]];
					ans = mapp[st][i];		//记录节点 
				}
			}
		}
	}
	return ans;
}
int main()
{
	int t1,t2,t3;
	for (int i = 0 ; i <= MAX ; i++)
	{
		mapp[i].clear();
		dis[i].clear();
	}
	n = 0;
	while (scanf ("%d %d %d",&t1,&t2,&t3) != EOF)
	{
		mapp[t1].push_back(t2);
		mapp[t2].push_back(t1);
		dis[t1].push_back(t3);
		dis[t2].push_back(t3);
		n = max(max(t1,t2),n);
	}
	if (n == 0)		//POJ的编译器没这个就RE,我的就可以(哭) 
	{
		printf ("0\n");
		return 0;
	}
	int st = dfs(n);
	int ans = dfs(st);
	printf ("%d\n",f[ans]);
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档