LightOJ 1094

Description:

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:

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Sample Output:

Case 1: 100

Case 2: 80

题意:有n个点,它们之间连接的边有权值,问两个点之间最大的权值是多少。(DFS或BFS求树的直径)

1.DFS:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int N=100010;

struct node
{
    int v, weight, num;
}no[N];
int head[N], Max, id, dist[N], k; ///Max保存最大的权值,id保存最大权值的终点,dist数组保存以每个点为终点的边的权值

void Add(int a, int b, int c) ///建立邻接表
{
    no[k].v = b;
    no[k].num = head[a];
    no[k].weight = c;

    head[a] = k++;
}

void DFS(int u, int w)
{
    int i, v;

    dist[u] = w;

    if (dist[u] > Max) ///更新最大权值及其终点
    {
        Max = dist[u];
        id = u;
    }

    for (i = head[u]; i != -1; i = no[i].num)
    {
        v = no[i].v;

        if (dist[v] == -1)
            DFS(v, dist[u]+no[i].weight);
    }
}

int main ()
{
    int T, n, i, a, b, c, cnt = 0;

    scanf("%d", &T);

    while (T--)
    {
        cnt++;

        memset(head, -1, sizeof(head));
        k = 0;

        scanf("%d", &n);
        for (i = 1; i < n; i++)
        {
            scanf("%d%d%d", &a, &b, &c);

            Add(a, b, c);
            Add(b, a, c);
        }

        Max = 0;
        memset(dist, -1, sizeof(dist));
        DFS(0, 0); ///第一次查找后找到一个最大值及其终点
        memset(dist, -1, sizeof(dist));
        DFS(id, 0); ///第二次查找以上一次查找的终点开始

        printf("Case %d: %d
", cnt, Max);
    }

    return 0;
}

2.BFS:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

const int N=100010;

struct node
{
    int v, weight, num;
}no[N];
int dist[N], head[N], k, Max, id;

void Add(int a, int b, int c)
{
    no[k].v = b;
    no[k].weight = c;
    no[k].num = head[a];

    head[a] = k++;
}

void BFS(int ans)
{
    int i, u, v;
    queue<int>Q;

    Q.push(ans);
    dist[ans] = 0;

    while (!Q.empty())
    {
        u = Q.front(); Q.pop();

        if (dist[u] > Max)
        {
            Max = dist[u];
            id = u;
        }

        for (i = head[u]; i != -1; i = no[i].num)
        {
            v = no[i].v;

            if (dist[v] == -1)
            {
                dist[v] = dist[u] + no[i].weight;
                Q.push(v);
            }
        }
    }
}

int main ()
{
    int T, n, i, a, b, c, cnt = 0;

    scanf("%d", &T);

    while (T--)
    {
        cnt++;
        
        k = 0;
        memset(head, -1, sizeof(head));

        scanf("%d", &n);
        for (i = 1; i < n; i++)
        {
            scanf("%d%d%d", &a, &b, &c);

            Add(a, b, c);
            Add(b, a, c);
        }

        Max = 0;
        memset(dist, -1, sizeof(dist));
        BFS(0);
        memset(dist, -1, sizeof(dist));
        BFS(id);

        printf("Case %d: %d
", cnt, Max);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/syhandll/p/4739387.html