lightoj1094 - Farthest Nodes in a Tree

本文探讨如何在给定的带权无向树中找到两点之间的最大距离,即树的直径。通过遍历树,首先找到离根节点最远的点作为起点,然后从该点出发再次遍历树以找到离它最远的点,最终得到树的最大距离。
1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s)Memory Limit: 32 MB

Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

Output for Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Case 1: 100

Case 2: 80

Notes

Dataset is huge, use faster i/o methods.


PROBLEM SETTER: JANE ALAM JAN
 

题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?

假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 #define N 30008
 8 
 9 struct node
10 {
11     int v, w, next;
12 }e[N*4];
13 
14 int n, cnt, maxx;
15 int Index;   // 写成index过不了lightoj=·=||
16 int head[N], dist[N];
17 
18 void addedge(int u, int v, int w)
19 {
20     e[cnt].v = v;
21     e[cnt].w = w;
22     e[cnt].next = head[u];
23     head[u] = cnt++;
24 }
25 
26 void dfs(int u, int w)
27 {
28     dist[u] = w;
29     if(w > maxx)
30     {
31         maxx = dist[u];
32         Index = u;
33     }
34     for(int i = head[u]; i != -1; i = e[i].next)
35     {
36         if(dist[e[i].v] == -1)
37         {
38             dfs(e[i].v, dist[u]+e[i].w);
39         }
40     }
41 }
42 int main()
43 {
44     int t, u, v, w, k = 1;
45 
46     scanf("%d", &t);
47 
48     while(t--)
49     {
50         cnt = maxx = 0;
51         memset(head, -1, sizeof(head));
52 
53         scanf("%d", &n);
54         n--;
55         while(n--)
56         {
57             scanf("%d%d%d", &u, &v, &w);
58             addedge(u, v, w);
59             addedge(v, u, w);
60         }
61         memset(dist, -1, sizeof(dist));
62         dfs(0, 0);
63         memset(dist, -1, sizeof(dist));
64         dfs(Index, 0);
65         printf("Case %d: %d\n", k++, maxx);
66     }
67     return 0;
68 }

bfs

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 #define N 30008
 9 
10 struct node
11 {
12     int v, w, next;
13 } e[N*2];
14 
15 int n, cnt, maxx;
16 int Index;
17 int head[N], dist[N], vis[N];
18 
19 void addedge(int u, int v, int w)
20 {
21     e[cnt].v = v;
22     e[cnt].w = w;
23     e[cnt].next = head[u];
24     head[u] = cnt++;
25 }
26 
27 void bfs(int u)
28 {
29     memset(vis, 0, sizeof(vis));
30     queue<int> Q;
31     Q.push(u);
32     vis[u] = 1;
33     dist[u] = 0;
34 
35     while(Q.size())
36     {
37         u = Q.front();
38         Q.pop();
39 
40         for(int i = head[u]; i != -1; i = e[i].next)
41         {
42             int v = e[i].v;
43             if(!vis[v])
44             {
45                 vis[v] = 1;
46                 dist[v] = dist[u]+e[i].w;
47                 if(dist[v] > maxx)
48                 {
49                     maxx = dist[v];
50                     Index = v;
51 
52                 }
53                 Q.push(v);
54             }
55         }
56     }
57 }
58 
59 int main()
60 {
61     int t, u, v, w, k = 1;
62     scanf("%d", &t);
63 
64     while(t--)
65     {
66         maxx = cnt = 0;
67         memset(head, -1,sizeof(head));
68 
69         scanf("%d", &n);
70         n--;
71 
72         while(n--)
73         {
74             scanf("%d%d%d", &u, &v, &w);
75             addedge(u, v, w);
76             addedge(v, u, w);
77         }
78         memset(dist, 0, sizeof(dist));
79         bfs(0);
80         bfs(Index);
81         printf("Case %d: %d\n", k++, maxx);
82     }
83     return 0;
84 }

 

转载于:https://www.cnblogs.com/Tinamei/p/4739674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值