Topic Links:
pku:http://poj.org/problem?id=1861
zju:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemid=542
Description
Andrew is working as system administrator and was 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 con Nection, 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 is to help Andrew to find the "the" to connect hubs so, all above conditions is satisfied.
Input
The first line of the input contains, numbers:n-the number of hubs in the network (2 <= N <=) 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 106. 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.
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 61 2 11 3 11 4 22 3 13 4 12 4 1
Sample Output
141 21 32) 33 4
Source
Northeastern Europe 2001, Northern subregion
Test instructions
There are n vertices, m-bars, each side is bidirectional, and has a certain length. Requires that each vertex be connected, and that the total length be the shortest,
Outputs the maximum edges, the total number of edges, and the selected edges.
Ps:
It seems that the case of the problem is a bit problematic, card for a long time!
The output should be:
1
3
1 3
2 3
2 4
The code is as follows:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 15017;int father[maxn];struct edge{int x,y,v;}; struct Edge Ed[maxn],ansa[maxn];bool cmp (Edge A,edge b) {return A.V<B.V;} int find (int x) {if (x==father[x]) return x; Return Father[x]=find (Father[x]);} void krusal (int n,int m) {int i,fx,fy,cnt; int ans=0; for (i = 1; I <= n; i++) father[i]=i; Sort (ed,ed+m,cmp);//The sort of side cnt=0; int max=-1; for (i=0; i<m; i++) {fx=find (ed[i].x); Fy=find (ED[I].Y); if (fx!=fy) {ans+=ed[i].v; Father[fx]=fy; ansa[cnt].x=ed[i].x; ANSA[CNT++].Y=ED[I].Y; if (MAX<ED[I].V) max=ed[i].v; }} printf ("%d\n%d\n", max,cnt); for (i=0; i<cnt; i++) printf ("%d%d\n", ansa[i].x,ansa[i].y);} int main () {int t; int n, m; int A, b, K; while (scanf ("%d%d", &n,&m)!=eof) { for (int i = 0; i < m; i++) {scanf ("%d%d%d", &a,&b,&k); Ed[i].x=a,ed[i].y=b,ed[i].v=k; } krusal (N,m); } return 0;}
POJ 1861 & ZOJ 1542 Network (Krusal of the smallest spanning tree)