Topic Connection:
problemid=542 "target=" _blank ">zoj 1542 POJ 1861 Network
Network Time limit: 2 Seconds Memory Limit: 65536 KB Special Judge
andrew is working as system administrator and is planning to establish a new network in he company. There'll is N hubs in the company, they can is connected to each of the other using cables. Since each worker of the company must has access to the whole network, each hub must is accessible by cables from any oth ER hub (with possibly some intermediate hubs).
Since Cables of different types be available and shorter ones are cheaper, it's necessary to make such a plan of Hub connection, that's the maximum length of a single cable is minimal. There is another problem-not each hub can being connected to any other one because of compatibility problems and building g Eometry limitations. Of course, Andrew would provide you all necessary information about possible hub connections.
You-to-help-Andrew-to-find the-the-connect hubs so, all above conditions is satisfied.
Input
The first line of the input file contains double 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 is numbered from 1 to N. The following M lines contain information about possible connections-the numbers of both hubs, which can be connected and The cable length required to connect them. Length is a positive integer number this does not exceed 10^6. There'll is no more than one-to-connect, hubs. A Hub cannot is connected to itself. There always is at least one and connect all hubs.
Process to the end of file.
Output
Output first the maximum length of a cable in your hub connection plan (the value of your should minimize). Then output your Plan:first output p-the number of cables used and then output P pairs of an integer numbers-numbers of hub s connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
Sample Input
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
Sample Output
1
4
1 2
1 3
2 3
3 4
Parse: Find the value of the longest edge in the minimum spanning tree. The selected edge is then output. Kruskal algorithm.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <cstdlib>using namespace std; #define MAXN 1010#define maxm 15002int N, M, MaxLen, Cnt;int parent[m AXN], ans[maxn];struct edge{int u, V, W;} Eg[maxm];bool CMP (Edge A, Edge b) {return A.W < B.W;} int Find (int x) {if (parent[x] = =-1) return x; Return Find (Parent[x]);} void Kruskal () {memset (parent,-1, sizeof (parent)); Sort (EG, eg+m, CMP); for (int i = 0; i < m; i++) {int T1 = find (eg[i].u), t2 = find (EG[I].V); if (t1! = t2) {if (MaxLen < EG[I].W) MaxLen = EG[I].W; ans[cnt++] = i; PARENT[T1] = T2; }}}int Main () {while (~scanf ("%d%d", &n, &m)) {for (int i = 0; i < m; i++) scanf ("%d %d%d ", &eg[i].u, &EG[I].V, &EG[I].W); MaxLen = CNT = 0; Kruskal (); printf ("%d\n", maxlen); Printf("%d\n", CNT); for (int i = 0; i < cnt; i++) printf ("%d%d\n", eg[ans[i]].u, EG[ANS[I]].V); } return 0;}
ZOJ 1542 POJ 1861 network minimum spanning tree, finding the longest edge, Kruskal algorithm