zoukankan      html  css  js  c++  java
  • LightOJ1094

    http://lightoj.com/volume_showproblem.php?problem=1094

    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

    树的直径典型题目 

    树的直径是让求给你一棵树 所有节点和他们的距离然后求这棵树上距离最远的两个点之间的距离

    分析:

    先是任意找一个点,然后bfs找到离他最远距离的点,把这个点标记起来

    然后用这个点再bfs一次找到直径

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    #include <vector>
    #include <queue>
    
    using namespace std;
    #define N 30005
    int ans,Max,Index;
    int head[N],vis[N],dis[N];
    struct node
    {
        int v,f,next;
    }e[N*4];
    
    void Inn()
    {
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        ans=Index=0;
    }
    void Add(int u,int v,int f)
    {
        e[ans].v=v;
        e[ans].f=f;
        e[ans].next=head[u];
        head[u]=ans++;
    }
    
    void bfs(int u)
    {
        Max=0;
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        queue<int>Q;
        Q.push(u);
        vis[u]=1;
        while(!Q.empty())
        {
            int p,q;
            p=Q.front();
            Q.pop();
            for(int i=head[p];i!=-1;i=e[i].next)
            {
                q=e[i].v;
                if(!vis[q])
                {
                    vis[q]=1;
                    Q.push(q);
                    dis[q]=dis[p]+e[i].f;
                    if(Max<dis[q])
                    {
                        Max=dis[q];
                        Index=q;
                    }
                }
            }
        }
    }
    
    int main()
    {
        int T,n,U,V,W,t=1;
        scanf("%d",&T);
        while(T--)
        {
            Inn();
            scanf("%d",&n);
            for(int i=1;i<n;i++)
            {
                scanf("%d %d %d",&U,&V,&W);
                Add(U,V,W);
                Add(V,U,W);
            }
            bfs(0);
            bfs(Index);
            printf("Case %d: %d
    ",t++,Max);
        }
        return 0;
    }
  • 相关阅读:
    基础数据类型之字符串str
    python编码基础知识
    python逻辑运算之and、or
    Django中消息中间键和form组件的运用
    Django中 cookies and session的使用
    JavaScript 正则制表符,单词边界,去空格
    paramiko堡垒机、线程及锁
    0911 Socket网络编程
    os.system和os.popen
    类高级方法、反射、异常、动态导入模块
  • 原文地址:https://www.cnblogs.com/linliu/p/5279145.html
Copyright © 2011-2022 走看看