Minimum Poj 1639 K Degree limit Spanning Tree

Source: Internet
Author: User

An undirected graph is given to find the minimum spanning tree that is obtained when the degree of the v0 vertex is not greater than K.
Question:
First, do not consider v0 point, first obtain the v1-v (n-1) MST, and then consider in two cases:
Set d to v0.
Case 1: When d = 1, the answer is obviously min {edge (0, I)} + MST {v1-v (n-1 )}
When 1 <d <= K, consider gradually adding a {0-i} edge. After adding the edge, it is bound to constitute a loop, and then find
Delete the edge with the largest weight in the MST and change it to {0-i}
Code: # include <stdio. h>
# Include <algorithm>
# Include <iostream>
# Include <string>
# Include <map>
Using namespace std;

Const int V = 21;
Const int INF = 1 <30;
Int n;

Struct Edge
{
Int from;
Int;
Int weight;
};

Map <string, int> Map;
Int graph [V] [V];
Edge edges [V];
Int vertexNum;
Int s;
Bool visited [V]; // whether the edge (0, I) is in edges

Void init ()
{
Memset (visited, 0, sizeof (visited ));
Map. clear ();
For (int I = 0; I <V; ++ I)
{
For (int j = 0; j <V; ++ j)
{
Graph [I] [j] = INF;
}
}
}


Void input ()
{
String name1, name2;
Int dis;
Map ["Park"] = 0;
Int k = 1;
For (int I = 0; I <n; ++ I)
{
Cin> name1> name2> dis;
If (Map. find (name1) = Map. end ())
Map [name1] = k ++;
If (Map. find (name2) = Map. end ())
Map [name2] = k ++;
Int id1 = Map [name1];
Int id2 = Map [name2];
Graph [id1] [id2] = graph [id2] [id1] = dis;
}
Scanf ("% d", & s );
}

// Obtain the minimum spanning tree of v0-v (_ vertexNum-1)
Int Prim (int _ vertexNum)
{
Int mstWeight = 0;
For (int I = 1; I <_ vertexNum-1; ++ I)
{
Edges [I]. from = 1;
Edges [I]. to = I + 1;
Edges [I]. weight = graph [1] [I + 1];
}
For (int I = 2; I <_ vertexNum; ++ I)
{
Int id = I-1;
Int minW = edges [I-1]. weight;
For (int j = I; j <_ vertexNum-1; ++ j)
{
If (minW> edges [j]. weight)
{
MinW = edges [j]. weight;
Id = j;
}
}
MstWeight + = minW;

Swap (edges [I-1], edges [id]);
Int k = edges [I-1].;

For (int j = I; j <_ vertexNum-1; ++ j)
{
Int v = edges [j].;
Int w = graph [k] [v];
If (w <edges [j]. weight)
{
Edges [j]. weight = w;
Edges [j]. from = k;
}
}
}
Return mstWeight;
}

// Return the largest edge in the loop
Bool isCycle;
Void maxWeightInCycle (int _ mv, int _ from, int _ to, int & _ maxW, int & _ id)
{
If (_ to = _ mv)
{
IsCycle = true;
Return;
}
For (int I = 0; I <vertexNum-1; ++ I)
{
If (edges [I]. from! = _ To & edges [I].! = _)
{
Continue;
}
If (edges [I]. from = _ to & edges [I].! = _ From)
{
MaxWeightInCycle (_ mv, _ to, edges [I]. to, _ maxW, _ id );
If (isCycle)
{
If (_ maxW <edges [I]. weight & edges [I].! = 0)
{
_ MaxW = edges [I]. weight;
_ Id = I;
}
Break;
}
}
Else if (edges [I]. to = _ to & edges [I]. from! = _ From)
{
MaxWeightInCycle (_ mv, _ to, edges [I]. from, _ maxW, _ id );
If (isCycle)
{
If (_ maxW <edges [I]. weight & edges [I]. from! = 0)
{
_ MaxW = edges [I]. weight;
_ Id = I;
}
Break;
}

Related Article

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.