Poj1639 picnic planning, K-degree limit Spanning Tree

Source: Internet
Author: User

Question:

Although a dwarf is small, he prefers to take a huge car. The car is as big as it can be loaded with any dwarf. One day, n (n ≤ 20) Dwarf plan to have dinner in the wild. In order to concentrate on the dinner place, dwarf a either drives to dwarf B's house, leaves his own car in dwarf B's house, and then walks in B's car; or directly drives to the dinner place, and parked the car at the dinner place. Although the dwarf's home is very large, there are no number of cars to park, but at most K cars can be parked at the dinner place. A weighted undirected graph is provided to describe the home and dinner location of N dwarf people and to find the shortest distance for all dwarf people to drive.


Minimum Spanning Tree with single-point K-degree Restriction
Algorithm steps:

1. Find the minimum forest generated by removing K-degree points, and set the number of forests to M.
2. connect the M tree and K-degree vertex with the shortest distance from K-degree vertex in each tree to generate a m-degree minimal spanning tree, the total answer is the sum of all edges of the Spanning Tree.
3. iterate K-m times, extend the M-degree spanning tree to K-degree Spanning Tree, and find the Minimum Spanning Tree Length.
(1) scan all adjacent points of a K-degree point (note: this is the source image) to find a point to make (the length of the maximum edge of the point to the K-degree point in the new generation tree) the maximum difference from (the distance between K-degree points in the source image. (Note: The vertex cannot be directly connected to the K-degree vertex in the Spanning Tree)
(2) if (1) the difference value is not greater than 0, you do not need to continue searching. Otherwise, connect the vertex to the K Degree point in the new spanning tree, replace the maximum edge, and then update the maximum edge from this point. At this time, the M-degree spanning tree is changed to m + 1-degree spanning tree, and the total answer minus this difference.
(3) loop through the above steps until the K-degree spanning tree or jumping out


# Include <map> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; const int maxn = 105; const int maxm = 100005; const int INF = 1e9; struct node {int V, W, next;} edge [maxm]; struct edge {int U, V, W; edge () {} edge (int u, int V, int W): U (u), V (V), w (w) {}} MX [maxn]; // stores the maximum edge int n, m, K, and sum from each point to the park point. // sum is the result int e, head [maxn], vis [maxn], dis [maxn], use [maxn] [maxn]; // The head is used for the neighboring table vis. The mark array DIS is used to find the smallest spanning tree. Use is used to mark whether there is an edge between two points int blocks, size [maxn], belong [maxn], pre [maxn]; // blocks indicates several connected blocks after the park is removed. The size is the number of connected blocks. Belong indicates which connected block the point belongs. PRE is used to record the edge int point [maxn] in the production tree. link [maxn]; // point indicates the point link closest to the park point in each connected block. It is the distance between the point and the park point. Map <string, int> MP; // voing name void Init () {e = 0, n = 1; blocks = 0, sum = 0; memset (Head,-1, sizeof head ); memset (VIS, 0, sizeof vis); memset (size, 0, sizeof siz E); memset (use, 0, sizeof use); For (INT I = 1; I <maxn; I ++) MX [I]. W = 0; memset (PRE, 0, sizeof pre); MP. clear ();} void insert (int x, int y, int W) {edge [e]. V = y; edge [e]. W = W; edge [e]. next = head [X]; head [x] = e ++;} int GETID (char s []) {If (MP. find (S) = MP. end () MP [s] = ++ N; return MP [s];} void DFS (int v) // This DFS divides the graph into some connected blocks {vis [v] = 1; Size [blocks] ++; belong [v] = blocks; for (INT I = head [v]; I! =-1; I = edge [I]. Next) if (! Vis [edge [I]. v]) DFS (edge [I]. v);} void prim (INT cur) // calculate the minimum spanning tree for a connected block {for (INT I = 1; I <= N; I ++) dis [I] = inf; For (INT I = 1; I <= N; I ++) // set a point in the block as the starting point to survive tree formation if (belong [I] = cur) {dis [I] = 0; break;} For (INT I = 1; I <= size [cur]; I ++) // The number of cycles is the number of vertices of the block, because this is slightly different from the general MST {int MI = inf, pos =-1; for (Int J = 1; j <= N; j ++) if (pre [J]! =-1 & mi> dis [J]) MI = dis [J], Pos = J; If (Pos! =-1) {sum + = mi; use [POS] [pre [POS] = use [pre [POS] [POS] = 1; // mark the edge pre [POS] =-1; for (Int J = head [POS]; J! =-1; j = edge [J]. Next) if (pre [edge [J]. V]! =-1 & dis [edge [J]. v]> edge [J]. w) {dis [edge [J]. v] = edge [J]. w; Pre [edge [J]. v] = POS ;}}} void getmax (int v, int FA, int W) // This function is used to update the maximum edge from the midpoint of the new spanning tree to the park point {pre [v] = fa; If (MX [fa]. w> W) MX [v] = Mx [fa]; else MX [v] = edge (v, fa, W); For (INT I = head [v]; i! =-1; I = edge [I]. Next) if (use [v] [edge [I]. V] & edge [I]. V! = FA) getmax (edge [I]. v, V, edge [I]. w); // it must be an edge in the Spanning Tree and not a back edge to search down} void getmdegreemst () {vis [1] = 1; for (INT I = 2; I <= N; I ++) // calculates the connected block if (! Vis [I]) {blocks ++; DFS (I);} Pre [1] =-1; for (INT I = 1; I <= blocks; I ++) prim (I); For (INT I = 1; I <= N; I ++) link [I] = inf; For (INT I = head [1]; I! =-1; I = edge [I]. next) // generate an M-degree spanning tree if (link [belong [edge [I]. v]> edge [I]. w) {link [belong [edge [I]. v] = edge [I]. w; point [belong [edge [I]. v] = edge [I]. V ;}for (INT I = 1; I <= blocks; I ++) // connects the park point to the nearest vertex in each connected block, and mark the edge {sum + = link [I]; use [1] [point [I] = use [point [I] [1] = 1 ;}} void slove () {int degree = blocks; getmax (1, 0, 0); // calculate the maximum edge again from the park point while (degree <K) // try to iterate K-degree times {int maxval = 0, Pos = 0, W; For (INT I = head [1]; I! =-1; I = edge [I]. Next) // locate the vertices with the greatest difference if (! Use [1] [edge [I]. v] & amp; MX [edge [I]. v]. w-edge [I]. w> maxval) {maxval = Mx [edge [I]. v]. w-edge [I]. w, Pos = edge [I]. v; W = edge [I]. w;} If (! Pos) break; sum-= maxval; // update the answer degree ++; use [MX [POS]. u] [MX [POS]. v] = use [MX [POS]. v] [MX [POS]. u] = 0; // Delete the maximum edge use [1] [POS] = use [POS] [1] = 1; getmax (Pos, 1, W ); // update the maximum edge} int main () {char S1 [55], S2 [55]; int W; scanf ("% d", & M ); init (); MP ["park"] = 1; for (INT I = 0; I <m; I ++) {scanf ("% S % d ", s1, S2, & W); insert (GETID (S1), GETID (S2), W); insert (GETID (S2), GETID (S1), W );} scanf ("% d", & K); getmdegreemst (); slove (); printf ("total miles driven: % d \ n", sum); Return 0 ;}


Poj1639 picnic planning, K-degree limit Spanning Tree

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.