POJ1861Network

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


解题思路:这题的意思是给你一些点和一些可能的链接,让你判断出所有边的权值和最的小的生成树的最大的边的值,和最小生成树的边有多少条,以及最小生成树的各个
边输出来。首先说明本题的测试数据比较坑,是错的,明显成环了,所以不要管测试数据。这题就是一个赤裸裸的克鲁斯卡尔算法题,但我在解这题的时候,发现用不同的排序方法得到的结果是不同的
用过的排序有冒泡、qsort,sort,用了各种排序,WA了无数次,最后用qsort过了,觉得这题好坑,连排序的选择不一样都能判出不同的结果。下面是AC代码


#include<cstdio> #include<iostream> #include <algorithm> using namespace std; int visit[15010],baocun[15100][2]; struct node {  int front,rear,length; }rode[15010]; int find(int i,int x) {  return (i==x? i:find(x,visit[x])); } void popsort(int n) {  for(int i=1;i<=n;++i)  for(int j=1;j<=n-1;++j)  if(rode[j].length>rode[j+1].length) {   node d=rode[j];   rode[j]=rode[j+1];   rode[j+1]=d;  } } int cmp(const void *_a,const void *_b) {  node *a=(node*)_a;  node *b=(node*)_b;  return a->length-b->length; }

int main() {  int N,M;  scanf("%d%d",&N,&M);  for(int i=0;i<=N;++i)  visit[i]=i;  for(int i=1;i<=M;++i)   scanf("%d%d%d",&rode[i].front,&rode[i].rear,&rode[i].length);   qsort(rode+1,M,sizeof(rode[0]),cmp);   int sum=0,max=0,z=0,num=1;   for(int i=1;i<=M;++i)   if(find(rode[i].front,visit[rode[i].front])!=find(rode[i].rear,visit[rode[i].rear])) {    if(num>=N-1)    break;    sum++;    if(rode[i].length>max)    max=rode[i].length;    baocun[++z][0]=rode[i].front;    baocun[z][1]=rode[i].rear;    visit[find(rode[i].front,visit[rode[i].front])]=rode[i].rear;   }   printf("%d\n%d\n",max,sum);   for(int i=N-1;i>=1;--i)   printf("%d %d\n",baocun[i][0],baocun[i][1]);  return 0; }

原文地址:https://www.cnblogs.com/xiaxiaosheng/p/3036683.html