POJ1861 Network

Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %lld & %llu

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 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

样例输出明显错了。

1

3

1 2

1 3

3 4

这样可行。有Special Judge

 

Source

Northeastern Europe 2001, Northern Subregion
 
 
裸最小生成树。要求输出生成树中最大边数,连接的点数(肯定是n-1),使用的每条边。
 
 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<queue>
 8 using namespace std;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 const int mxn=16000;
16 int n,m;
17 struct node{
18     int x,y;
19     int dis;
20 }e[mxn];
21 int cnt=0;
22 int cmp(node a,node b){
23     return a.dis<b.dis;
24 }
25 int fa[mxn];
26 void init(){
27     for(int i=1;i<=n;i++)fa[i]=i;
28 }
29 int find(int x){
30     if(fa[x]==x)return x;
31     return fa[x]=find(fa[x]);
32 }
33 queue<int>aw;
34 int ans=0;
35 void Kruskal(){
36     int i,j;
37     ans=0;
38     int now=0;
39     for(i=1;i<=m;i++){
40         int u=find(e[i].x),v=find(e[i].y);
41         if(u==v)continue;
42         aw.push(i);
43         fa[u]=v;
44         now++;
45         ans=max(ans,e[i].dis);
46         if(now==n-1)break;
47     }
48 }
49 int main(){
50     while(scanf("%d",&n)!=EOF){
51         init();
52         int u,v,dis;
53         m=read();
54         int i,j;
55         for(i=1;i<=m;i++){
56             e[i].x=read();e[i].y=read();e[i].dis=read();
57         }
58         sort(e+1,e+m+1,cmp);
59         Kruskal();
60         printf("%d
",ans);
61         printf("%d
",n-1);
62         while(!aw.empty()){
63             int tmp=aw.front();
64             aw.pop();
65             printf("%d %d
",e[tmp].x,e[tmp].y);
66         }
67     }
68     return 0;
69 }
原文地址:https://www.cnblogs.com/SilverNebula/p/5892664.html