The longest side of the Spanning Tree requested by kruscal is as small as possible, which is different from Prim's network.
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:10476 |
|
Accepted:3945 |
|
Special Judge |
Description
Andrew is working as system administrator and is planning to establish a new network in his company. there will be n hubs in the company, they can be connected to each other using cables. since each worker of the company must have
Access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs ).
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of Hub Connection, that the maximum length of a single cable is minimal. there is another problem-not each hub can be connected to any other one
Because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to Connect hubs so that all above conditions are satisfied.
Input
The first line of the input contains two integer numbers: N-the number of hubs in the network (2 <=n <= 1000) and M-the number of possible hub connections (1 <= m <= 15000 ). all hubs are numbered from 1 to n. the following
M lines contain information about possible connections-the numbers of two hubs, which can be connected and the cable length required to connect them. length is a positive integer number that does not exceed 10 6 .
There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.
Output
Output first the maximum length of a single cable in your Hub Connection plan (the value you should minimize ). then output your plan: first output p-the number of cables used, then output p pairs of integer numbers-Numbers
Of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
Sample Input
4 61 2 11 3 11 4 22 3 13 4 12 4 1
Sample output
141 21 32 33 4
# Include <iostream> # include <vector> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> # include <cmath> # include <iomanip> # include <complex> using namespace STD; # define maxn 1100 struct line {int P1, P2; int dis ;}; line L [maxn * 20], que [maxn]; int N, set [maxn], M; int maxs, num; bool CMP (line A, line B) {return. dis <B. DIS;} void Init (int n) {for (INT I = 0; I <= N; I ++) set [I] = I ;} // search for the set number of A: int find (int A) {int Root = A; int temp; while (set [root]! = Root) root = set [root]; while (set [a]! = Root) {temp = A; A = set [a]; set [temp] = root;} return root;} // merge two sets bool Merge (int, int B) {int x = find (a); int y = find (B); If (x = y) return false; set [x] = y; return true;} void Kruskal () {for (INT I = 0; I <M & num <n-1; I ++) {If (merge (L [I]. p1, L [I]. p2) {Maxs = L [I]. DIS; num ++; que [num]. p1 = L [I]. p1; que [num]. p2 = L [I]. p2 ;}} void solve () {Init (n); For (INT I = 0; I <m; I ++) scanf ("% d", & L [I]. p1, & L [I]. p2, & L [I]. dis); sort (L, L + M, CMP); Maxs = 0, num = 0; Kruskal (); printf ("% d \ n", Maxs ); printf ("% d \ n", num); For (INT I = 1; I <= num; I ++) printf ("% d \ n ", que [I]. p1, que [I]. p2);} int main () {While (CIN> N> m) solve (); Return 0 ;}