POJ2631——Roads in the North
2014-10-17 18:58
477 查看
Roads in the North
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
Source
The UofA Local 1999.10.16
求树的直径
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2192 | Accepted: 1073 |
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
The UofA Local 1999.10.16
求树的直径
#include <map> #include <set> #include <list> #include <stack> #include <vector> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 10010; const int inf = 0x3f3f3f3f; struct node { int weight; int next; int to; }edge[N << 2]; int n, m; int end_p; bool vis ; int dist ; int head ; int tot; int maxs; void addedge(int from, int to, int weight) { edge[tot].weight = weight; edge[tot].to = to; edge[tot].next = head[from]; head[from] = tot++; } void build() { char dir[4]; int u, v, w; memset( head, -1, sizeof(head) ); tot = 0; for (int i = 0; i < m; ++i) { scanf("%d%d%d%s", &u, &v, &w, dir); addedge(u, v, w); addedge(v, u, w); } } void bfs(int s) { queue<int>qu; memset( vis, 0, sizeof(vis) ); while ( !qu.empty() ) { qu.pop(); } qu.push(s); dist[s] = 0; vis[s] = 1; maxs = 0; while ( !qu.empty() ) { int u = qu.front(); qu.pop(); for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (!vis[v]) { vis[v] = 1; dist[v] = dist[u] + edge[i].weight; qu.push(v); if (maxs < dist[v]) { maxs = dist[v]; end_p = v; } } } } } int main() { int u, v, w; memset (head, -1, sizeof(head) ); tot = 0; while (scanf("%d%d%d", &u, &v,&w) == 3) { addedge(u, v, w); addedge(v, u, w); } bfs(1); bfs(end_p); printf("%d\n", maxs); return 0; }
相关文章推荐
- POJ - 2631 Roads in the North
- 【POJ】2631 - Roads in the North(树的直径)
- 【POJ】-2631-Roads in the North(树的直径)
- POJ 2631 -- Roads in the North【树的直径 && 裸题】
- POJ 2631 Roads in the North【裸题】
- Roads in the North(POJ 2631 DFS)
- POJ 2631 Roads in the North 树的直径
- POJ-2631 Roads in the North(最大路径)
- 【树的直径】 POJ 2631 Roads in the North
- 【POJ 2631 Roads in the North】
- poj--2631--Roads in the North(树的直径 裸模板)
- poj 2631 Roads in the North
- POJ 2631 Roads in the North(树形dp)
- POJ:2631 Roads in the North(树的最大直径)
- POJ-2631-Roads in the North-树的直径(最长路)
- POJ Problem 2631 Roads in the North【树的直径】
- 树的直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North
- POJ 2631 Roads in the North
- poj 2631 Roads in the North(裸树的直径)
- POJ 2631 Roads in the North(村庄最大距离,树的直径,经典题目)