Ural 1416 confidential subgeneration tree

Source: Internet
Author: User

/*

Question:

For a very bare sub-tree question, the Minimum Spanning Tree value must be given first. If there is an output that is not connected-1,

Then we need to output the next small Spanning Tree value.

Analysis:

Select prim or Kruskal.AlgorithmI use prim to do this. For details, seeCodeNote

 

*/

# Include <iostream>

# Include <cstring>

# Include <cstdio>

Using namespace STD;

# Define x 503

# Define INF 10000000

Int path [x] [X], map [x] [X], DIS [X], pre [X], n, m;

Bool use [X], visit [x] [x];

Int prim ()

{

Memset (PRE, 0, sizeof (pre); // frontend Vertex

Memset (visit, false, sizeof (visit); // indicates whether the edge is in the minimal spanning tree.

Memset (use, false, sizeof (use); // indicates whether the edge is in the Minimum Spanning Tree.

Memset (path, 0, sizeof (PATH); // edge that marks the maximum length between the two vertices

For (INT I = 1; I <= N; I ++)

Dis [I] = inf;

Dis [1] = 0;

Int K, Min, ANS = 0;

For (INT I = 0; I <n; I ++)

{

Min = inf;

For (Int J = 1; j <= N; j ++)

If (! Use [J] & dis [J] <min)

Min = dis [k = J];

If (min = inf) // if there is no connection path, exit directly.

Return INF;

 

Int P = pre [k];

Visit [p] [k] = visit [k] [p] = true; // indicates that the edge is in the Spanning Tree.

Path [p] [k] = min; // represents the minimum length of the edge in the Spanning Tree.

For (Int J = 1; j <= N; j ++)

If (use [J]) // update the distance from all the points in the Minimum Spanning Tree to K. path [J] [k]

Path [J] [k] = path [J] [p]> path [p] [k]? Path [J] [p]: path [p] [k];

Ans + = min;

Use [k] = true;

For (Int J = 1; j <= N; j ++)

If (! Use [J] & dis [J]> map [k] [J])

Dis [J] = map [k] [J], pre [J] = K; // if K can update dis [J], K indicates that K is the PRE of J.

}

Return ans;

}

Int main ()

{

Freopen ("sum. In", "r", stdin );

Freopen ("sum. Out", "W", stdout );

Int x, y, z;

While (CIN> N> m)

{

For (INT I = 1; I <= N; I ++)

For (Int J = 1; j <= N; j ++)

Map [I] [J] = inf;

While (M --)

{

Scanf ("% d", & X, & Y, & Z );

Map [x] [Y] = map [y] [x] = z;

}

Int ans = prim (); // obtain the minimum spanning tree.

If (ANS = inf)

{

Cout <"cost:" <-1 <Endl;

Cout <"cost:" <-1 <Endl;

}

Else

{

Cout <"cost:" <ans <Endl;

Int ans2 = inf;

For (INT I = 1; I <= N; I ++)

For (Int J = 1; j <= N; j ++)

If (! Visit [I] [J] & map [I] [J] <inf)

{// Enumeration edge. If the edge is not in the Minimum Spanning Tree and is connected, replace it with an edge in the Minimum Spanning Tree.

Int temp = ans + map [I] [J]-path [I] [J];

If (temp <ans2)

Ans2 = temp;

}

If (ans2 = inf)

Cout <"cost:" <-1 <Endl;

Else

Cout <"cost:" <ans2 <Endl;

}

}

Return 0;

}

 

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.