The nucleus of the noip2007 tree network

Source: Internet
Author: User

The nucleus of the P1099 tree network
      • by
      • 221 Submit
    • Topic provider The user does not exist
    • Label Dynamic planning tree structure Noip raising group
    • Difficulty Increase +/province selection-

Submit a discussion of the problem record

Title Description

Set t= (V, E, W) is a non-circle and connected undirected graph (also known as a root tree), each edge to have a positive integer right, we call T a tree net (treebetwork), wherein the v,e respectively represents a set of nodes and edges, W represents the set of each edge length, and set T has n nodes.

Path: Any two nodes in the tree network, A and B, have a unique simple path, with D (A, b) as the length of the path to a, and the end of the endpoint, which is the sum of the lengths of each side of the path. We call D (A, b) the distance between A and b two nodes.

D (V, P) =min{d (V, u), U is the node on path P}.

Tree Net Diameter: the longest path in the tree network becomes the diameter of the tree net. For a given tree mesh t, the diameter is not necessarily unique, but it can be proved that: the midpoint of each diameter (not necessarily exactly a node, may be in the interior of an edge) is unique, we call this point is the center of the Tree network.

Eccentricity ECC (F): The distance from the farthest node in the tree mesh t to the path F, i.e.

ECC (f) =max{d (V, f), v∈v}

Task: For a given tree net t= (V, E, W) and nonnegative integer s, find a path F, which is a path on a certain diameter (the path both ends are nodes in the tree network), whose length does not exceed s (can be equal to s), so that the eccentricity of ECC (F) is minimized. We call this path the core (core) of the tree mesh t= (V, E, W). If necessary, F can degenerate into a node. In general, under the above definition, the nucleus is not necessarily the only one, but the minimum eccentricity is unique.

The following figure shows an example of a tree network. In the figure, A-B and a-c are two diameters and each length is 20. Point W is the center of the tree net, and the EF side is 5 in length. If s=11 is specified, the kernel of the tree network is the path DEFG (which can also be taken as Path Def) with an eccentric distance of 8. If you specify S=0 (or S=1, s=2), the core of the tree mesh is node F, and the eccentricity is 12.

Input output Format input format:

The input file core.in contains n rows:

Line 1th, two positive integers n and S, separated by a space. where n is the number of nodes in the tree network, S is the upper bound of the length of the kernel of the tree network. Set the node number to 1,2,......,n.

From line 2nd to nth row, each row gives 3 positive integers separated by spaces, which in turn represent the two endpoint numbers and lengths for each edge. For example, "2 4 7" indicates the length of the edge of the Connection node 2 and 4 is 7.

The data given is for the fight, not to be tested.

Output format:

The output file Core.out has only a nonnegative integer, which is the minimum eccentricity in the specified sense.

Input and Output Sample input example # #:
"Input Sample 1" 5 21 2 52 3 22 4 42 5 3 "Input Sample 2" 8 61 3 22 3 2 3 4 64 5 34 6 44 7 27 8 3
Sample # # of output:
"Output Sample 1" 5 "output Example 2" 5
Description

40% of the data meet: 5<=n<=15

70% of the data meet: 5<=n<=80

100% of the data meet: 5<=n<=300,0<=s<=1000. A positive integer with an edge length of not more than 1000

NOIP 2007 Raising the fourth question

Analysis: The topic is very long, a variety of concepts are very messy, we follow the title of a piece of the request, first how to find out the diameter? Because the requirements of the furthest two points, so must be the distance between every two points, you can use SPFA, but the data is less, considering the SPFA algorithm is more complex so with the Floyd algorithm to find out the diameter, we require in diameter to meet the requirements of the line segment, How do you know if a point I is on the diameter l-r? Set D[I][J] for the shortest distance of i,j, then if d[l][i] + d[i][r] = = D[l][r], then this point I must be on the diameter of l-r. In fact, to find the diameter of a method, that is to take a point u,dfs to find the farthest point L, and then DFS to find the furthest point of L R, So l-r is a diameter, based on this method, we know that the diameter of the point must be two points from the diameter of the farthest distance, Otherwise, this is not a diameter. We enumerate a line segment, the eccentricity must be one end of the line segment on the diameter of an endpoint to the minimum value of the maximum value, and constantly update the optimal value, you can find the eccentric distance. Because there are a lot of bars in diameter, and the eccentric distance is only one, so only one diameter is required.

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector> #define INF 1<<30using namespaceStd;intN,s,ans;int d[310][310];vector <pair<int,int> >V1,v2;bool vis[310][310];intMain () {memset (vis,false,sizeof(VIS)); scanf ("%d%d",&n,&s); for (int i = 1; I <= N; i++) for (int j = 1; J <= N; j + +) if (I! =j) D[i][j] =Inf for (int i = 0; i < n-1; i++) {intU,v,w; scanf ("%d%d%d",&u,&v,& W); D[v][u] = D[u][v] =  w;} for (int k = 1, k <= N; k++ ) for (int i = 1; I <= n; i++ ) for (int j = 1; J <= N; J + + ) if (D[i][k] < inf && d[k][j] <  inf) d[i][j] = min (d[i][j],d[i][k] +  d[k][j]); int Max i = 0 , l,r; for (int i = 1; I <= n; i++ ) for (int j = 1; J <= N; j + + ) if (d[i][j]! = INF && D[I][J] ( maxi) {maxi =  d[i][j]; l = i,r =  J;} ans =  inf; int ECC = 0 ; for (int i = 1; I <= N; i++ ) if (D[l][i] + d[i][r] = =  D[l][r]) for (int j = 1; J <= N; j + + ) if (D[l][j] + d[j][r] = =  d[ L][r]) {if (d[i][j];  s) continue ; ECC =  max (min (d[i][l],d[j][l]), Min (d[r][i],d[r][j])); Ans = min (ANS,ECC); } printf ("%d" , ans); return 0 ;}            

Spit Groove: Made a very silly mistake, unexpectedly will two INF value directly add, alas, or do too few questions.

The nucleus of the noip2007 tree network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.