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;
    }
  • 相关阅读:
    替换内容里面的图片
    mysql字符串拼接
    判断字符串中中是否有手机号
    验证身份证号码的真伪
    m端访问pc端 让跳到对应m端
    百度编辑器实现页面关闭再次打开内容处在已编辑状态
    如何实现 antd table 自动调整可视高度(纵向滚动条,scrollY)
    Flink 1.12.1 NoClassDefFoundError SourceFunction
    Java8 常用时间转换工具类
    Jenkins脚本清理构建历史
  • 原文地址:https://www.cnblogs.com/linliu/p/5279145.html
Copyright © 2011-2022 走看看