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;
    }
  • 相关阅读:
    Python 初学者必看:Python 异常处理集合
    解析分布式应用框架Ray架构源码
    混合编程:如何用python11调用C++
    AI开发效率低,你可以试试华为NAIE AutoML
    知道Python中的字符串是什么吗?
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    MindSpore部署图像分割示例程序
    MindSpore模型推理
    MindSpore特性支持类
    MindSpore后端运行类
  • 原文地址:https://www.cnblogs.com/linliu/p/5279145.html
Copyright © 2011-2022 走看看