poj 1861 Network 最小生成树

Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 12839 Accepted: 4937 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 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
3
1 2
1 3
3 4

====================================================================================

题目大意:公司要用网线把交换机都连起来,连成一个最小生成树,回答的时候第一行是最小生成树里边权最大的一条边,第二行是边数,后面是各条边连接的点对(虽然没要求排序我还是按左边的点升序排了下)。因为不是求边权和所以题目里说边权能到100w不足为虑。


解题思路:求最小生成树意思很明显了,给边是散边的形式,邻接表比较合适,也就是要用Kruskal。

p.s.:poj这道题的Sample Output是错的,当时看见吓得我一身冷汗以为审题有误,我这里改过来了。

//Memory: 864K	Time: 110MS

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct edge                                             //用来存条件里的边集
{
    edge( int A , int B , int V ):a(A),b(B),v(V){}      //写好构造函数方便插入vector
    int a , b , v;
    bool operator < ( const edge &e )const              //重载小于号方便sort排序
    {
        return v < e.v;
    }
};
struct ansEdges                                         //不带权值的边,用来存选中的边集
{
    ansEdges(int A ,int B):a(A),b(B){}                  //写好构造函数方便插入vector
    int a , b;
    bool operator < ( const ansEdges &e )const          //重载小于号方便sort排序
    {
        return a < e.a;
    }
};
vector<edge> l;
vector<ansEdges> ansL;
int father[1001] , ans , eN;
//并查集
int getFather(int x)
{
    return (!father[x])?x:( father[x] = getFather(father[x]) );
}

void Union(int x , int y , int v)
{
    int fx = getFather(x);
    int fy = getFather(y);
    if( fx == fy )
        return;
    father[fy] = fx;
    if( v > ans )                           //这里更新最大边权
        ans = v;
    ansL.push_back(ansEdges(x,y));          //加入选中的边集
    eN++;
}
//Kruskal算法
void Kruskal(int n)
{
    vector<edge>::iterator it;
    for( it = l.begin() ; it != l.end() ; it++ )
    {
        if( eN == n-1 )
            return;
        Union(it->a,it->b,it->v);
    }
}

int main()
{
    int N , M;
    scanf("%d%d", &N, &M);
    while(M--)
    {
        int a , b , v;
        scanf("%d%d%d", &a, &b, &v);
        l.push_back(edge(a,b,v));                   //加入条件边集
    }
    sort(l.begin(),l.end());                        //条件边集升序排序
    Kruskal(N);
    sort(ansL.begin(),ansL.end());                  //选中边集升序排序
    printf("%d\n%d\n", ans, ansL.size());
    vector<ansEdges>::iterator it;
    for( it = ansL.begin() ; it != ansL.end() ; it++ )
        printf("%d %d\n", it->a, it->b);
    return 0;
}
难得oj言而有信真只有一组数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值