您的位置:首页 > Web前端 > Node.js

lightoj1094 - Farthest Nodes in a Tree

2015-08-18 16:05 561 查看
1094 - Farthest Nodes in a Tree

PDF (English)StatisticsForum
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最远的点就是树上最远的距离

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define N 30008

struct node
{
int v, w, next;
}e[N*4];

int n, cnt, maxx;
int Index;   // 写成index过不了lightoj=·=||
int head
, dist
;

void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}

void dfs(int u, int w)
{
dist[u] = w;
if(w > maxx)
{
maxx = dist[u];
Index = u;
}
for(int i = head[u]; i != -1; i = e[i].next)
{
if(dist[e[i].v] == -1)
{
dfs(e[i].v, dist[u]+e[i].w);
}
}
}
int main()
{
int t, u, v, w, k = 1;

scanf("%d", &t);

while(t--)
{
cnt = maxx = 0;
memset(head, -1, sizeof(head));

scanf("%d", &n);
n--;
while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, -1, sizeof(dist));
dfs(0, 0);
memset(dist, -1, sizeof(dist));
dfs(Index, 0);
printf("Case %d: %d\n", k++, maxx);
}
return 0;
}


bfs

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

#define N 30008

struct node
{
int v, w, next;
} e[N*2];

int n, cnt, maxx;
int Index;
int head
, dist
, vis
;

void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}

void bfs(int u)
{
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(u);
vis[u] = 1;
dist[u] = 0;

while(Q.size())
{
u = Q.front();
Q.pop();

for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].v;
if(!vis[v])
{
vis[v] = 1;
dist[v] = dist[u]+e[i].w;
if(dist[v] > maxx)
{
maxx = dist[v];
Index = v;

}
Q.push(v);
}
}
}
}

int main()
{
int t, u, v, w, k = 1;
scanf("%d", &t);

while(t--)
{
maxx = cnt = 0;
memset(head, -1,sizeof(head));

scanf("%d", &n);
n--;

while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, 0, sizeof(dist));
bfs(0);
bfs(Index);
printf("Case %d: %d\n", k++, maxx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: