zoukankan      html  css  js  c++  java
  • Light OJ 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

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<vector>
     6 #include<algorithm>
     7 using namespace std;
     8 const int maxn=3e4+5;
     9 struct Edge
    10 {
    11     int v,w;
    12 };
    13 struct node
    14 {
    15     int u,tot;
    16 };
    17 bool vis[maxn];
    18 int ans,ed;
    19 vector<Edge>G[maxn];
    20 void bfs(int u)
    21 {
    22     vis[u]=1;
    23     queue<node>q;
    24     node t1,t2;
    25     t1.u=u,t1.tot=0;
    26     q.push(t1);
    27     while(!q.empty())
    28     {
    29         t1=q.front();q.pop();
    30         if(ans<t1.tot)ans=t1.tot,ed=t1.u;
    31         int len=G[t1.u].size();
    32         for(int i=0;i<len;i++)
    33         {
    34             int v=G[t1.u][i].v;
    35             if(vis[v])continue;
    36             t2.u=v,t2.tot=G[t1.u][i].w+t1.tot;
    37             q.push(t2);
    38             vis[v]=1;
    39         }
    40     }
    41 }
    42 int main()
    43 {
    44     int T;
    45     scanf("%d",&T);
    46     for(int kase=1;kase<=T;kase++)
    47     {
    48         int n;
    49         ans=0;
    50         scanf("%d",&n);
    51         for(int i=0;i<n;i++)G[i].clear();
    52         for(int i=0;i<n-1;i++)
    53         {
    54             int u,v,w;
    55             scanf("%d%d%d",&u,&v,&w);
    56             G[u].push_back((Edge){v,w});
    57             G[v].push_back((Edge){u,w});
    58         }
    59         memset(vis,0,sizeof(vis));
    60         bfs(0);
    61         memset(vis,0,sizeof(vis));
    62         bfs(ed);
    63         printf("Case %d: ",kase);
    64         printf("%d
    ",ans);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    Git ignore file for Xcode projects
    How can I create a zip archive of a whole directory via terminal without hidden files?
    What is a bare git repository?
    How to tell if UIViewController's view is visible
    Adding A Shadow To UIView
    Properties
    iOS中nil,Nil,NULL之间的区别
    FMDB的简单使用
    iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
    对retain 和 assign的理解
  • 原文地址:https://www.cnblogs.com/homura/p/5421439.html
Copyright © 2011-2022 走看看