UVA10308 POJ2631 Roads in the NorthDFS
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10308 POJ2631 Roads in the NorthDFS相关的知识,希望对你有一定的参考价值。
Roads in the North
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7827 Accepted: 3603
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
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22
Source
问题链接:UVA10308 POJ2631 Roads in the North
问题简述:给定一些村庄的距离,求哪2个村庄之间的距离最长。
问题分析:类似与求树的直径,用DFS来解决。也可以使用最短路径算法来解决。
程序说明:POJ与UVA的输入数据不同,代码略有不同。
参考链接:(略)
题记:(略)
AC的C++语言程序(UVA)如下:
/* UVA10308 Roads in the North */
#include <bits/stdc++.h>
using namespace std;
const int N = 10000 + 1;
vector<pair<int, int> > g[N];
int ans, vis[N];
int dfs(int cur)
{
vis[cur] = 1;
int t = 0;
for (int i = 0; i < (int)g[cur].size(); i++)
if (vis[g[cur][i].first] == 0) {
int sum = dfs(g[cur][i].first) + g[cur][i].second;
ans = max(ans, sum + t);
t = max(t, sum);
}
vis[cur] = 0;
return t;
}
int main()
{
string s;
while (getline(cin, s)) {
int u, v, w;
for (int i = 0; i < N; i++)
g[i].clear();
while (s[0] != '\\0') {
sscanf(s.c_str(), "%d%d%d", &u, &v, &w);
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
getline(cin, s);
}
memset(vis, 0, sizeof vis);
ans = 0;
dfs(1);
printf("%d\\n", ans);
}
return 0;
}
AC的C++语言程序(POJ)如下:
/* POJ2631 Roads in the North */
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 10000 + 1;
vector<pair<int, int> > g[N];
int ans, vis[N];
int dfs(int cur)
{
vis[cur] = 1;
int t = 0;
for (int i = 0; i < (int)g[cur].size(); i++)
if (vis[g[cur][i].first] == 0) {
int sum = dfs(g[cur][i].first) + g[cur][i].second;
ans = max(ans, sum + t);
t = max(t, sum);
}
vis[cur] = 0;
return t;
}
int main()
{
for (int i = 0; i < N; i++)
g[i].clear();
int u, v, w;
while (~scanf("%d%d%d", &u, &v, &w)) {
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
memset(vis, 0, sizeof vis);
ans = 0;
dfs(1);
printf("%d\\n", ans);
return 0;
}
以上是关于UVA10308 POJ2631 Roads in the NorthDFS的主要内容,如果未能解决你的问题,请参考以下文章
Poj2631--Roads in the North(树的直径)
题解报告:poj 2631 Roads in the North(最长链)