博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1861 Network
阅读量:4978 次
发布时间:2019-06-12

本文共 2994 字,大约阅读时间需要 9 分钟。

Network
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10104   Accepted: 3796   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

Source

, Northern Subregion
//开始还以为样例错了、呵呵、
//考验英语水平、、呵呵

 

#include <iostream>

#include <stdio.h>
#include <string.h>
#define M 150003
#define N 1003
#include <algorithm>
using namespace std;
struct node
{
    int u,v,cost;
};
node kr[M];
bool cmp(const node&a,const node&b)
{
    return a.cost<b.cost;
}
int n,m;
int f[N],r[N];
int Find(int x)
{
    if(x!=f[x])
      return f[x]=Find(f[x]);
    return x;
}
int E[N];
void kruskal()
{
    int i;
    int Max,k;
    for(i=1;i<=n;i++)
      f[i]=i,r[i]=0;
    sort(kr,kr+m,cmp);
    Max=k=0;
    int x,y;
    for(i=0;i<m;i++)
    {
       x=Find(kr[i].u);
       y=Find(kr[i].v);
       if(x!=y)
       {
           E[k++]=i;
          if(kr[i].cost>Max)
             Max=kr[i].cost;
         if(r[x]>r[y])
           f[y]=x;
          else if(r[y]>r[x])
               f[x]=y;
               else
               {
                   f[y]=x;
                   r[x]++;
               }
       }
    }
        printf("%d\n",Max);
        printf("%d\n",k);
        for(int j=0;j<k;j++)
         printf("%d %d\n",kr[E[j]].u,kr[E[j]].v);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<m;i++)
         scanf("%d%d%d",&kr[i].u,&kr[i].v,&kr[i].cost);
         kruskal();
    }
    return 0;
}

转载于:https://www.cnblogs.com/372465774y/archive/2012/07/12/2588344.html

你可能感兴趣的文章