Minimum Spanning Tree of poj2485

Source: Internet
Author: User

Problem: poj2485

The minimum value of the maximum edge of the Spanning Tree

Analysis:

First, it is proved that the minimum value of the maximum edge of the spanning tree is the maximum edge of the least spanning tree.

Assume that the minimum value of the maximum edge of the Spanning Tree is smaller than that of the Minimum Spanning Tree.

Set C to a minimum spanning tree of G, where E is the largest edge. If e is removed from C, C is divided into C1 and C2 connected subsets. Assume that the maximum edge is less than e of the Spanning Tree CC, the connection C1, C2 two point set of the bridge EE must be less than E. Replace E in C with ee, then C1, C2, ee must also generate a spanning tree. The length of the Spanning Tree is less than C, which is in conflict with the fact that C is the smallest spanning tree. Therefore, this assumption is not true.

Therefore, it must be:

Minimum value of the maximum edge of the Spanning Tree =Maximum edge of the Minimum Spanning Tree.

Therefore, this question is converted to the Minimum Spanning Tree for solving known graphs. Use the prim method.

AC code:
 1 //Memory: 532K        Time: 172MS 2 #include <iostream> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6  7 const int maxn = 501; 8 int g[maxn][maxn]; 9 bool vis[maxn];10 int d[maxn];11 int n, t; 12 int _min, _max;13 14 void prim()15 {16     memset(vis, 0, sizeof(vis));17     vis[0] = 1;18     int size = 1;19     _max = 0;20     for (int i = 1 ; i < n; i++){21         d[i] = g[0][i];22     }23     while (size < n) {24         _min = 65536;25         int k;26         for (int i = 1; i < n; i++) {27             if ( !vis[i] && d[i] < _min) {28                 _min = d[i];29                 k = i;30             }31         } 32         vis[k] = 1;33         if (_min > _max) _max = _min;34 35         for (int i = 0; i < n; i++) {36             if ( !vis[i] && g[i][k] < d[i])37                 d[i] = g[k][i];38         }39         size++;40     }41 }42 43 int main()44 {45     scanf("%d", &t);46     while (t--){47         scanf("%d", &n);48         for (int i = 0; i < n; i++)49             for (int j = 0; j < n; j++)50                 scanf("%d", &g[i][j]);51         prim();52         printf("%d\n", _max);53     }54     return 0;55 }

 

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.