1. 程式人生 > >kruskal (最小瓶頸生成樹)poj 1861

kruskal (最小瓶頸生成樹)poj 1861

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

題意:
給你一個N個點和M條邊的圖,現在要你從這M條邊中選一些邊的集合,使得單邊的長度的最大值最小且所有N個點要連通.要你輸出:單邊長度的最大值,選的邊數目,每條邊的兩個端點號.

分析:

和找最小生成樹一樣找到最小生成樹輸出最大的一條邊。

程式碼:
這個程式碼,本人在poj上用c++提交錯誤,換成g++ A了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=50000;
struct edge
{
    int u,v,dis;
    edge(int u1,int v1,int dist):u(u1),v(v1),dis(dist){}
    edge(){}
};
bool com(edge edge1,edge edge2)
{
    return edge1.dis<edge2.dis;
}
struct kruskal
{
    int n,m;
    edge edges[maxn];
    vector<int> e;
   int fa[maxn];
    int intn(int n)
    {
        this->n=n;
        e.clear();
        m=0;
        memset(fa,-1,sizeof(fa));
    }
    int findest(int u)
    {
        if(fa[u]==-1)return u;
        else
        {
            fa[u]=findest(fa[u]);
        }
        return fa[u];
    }
    void addedge(int u,int v,int dis)
    {
        edges[m++]=edge(u,v,dis);
    }
    int Kruskal()
    {
        sort(edges,edges+m,com);
        int cnt=0;
        for(int i=0;i<m;i++)
        {
            int u=findest(edges[i].u);
            int v=findest(edges[i].v);
            if(u!=v)
            {
                e.push_back(i);
                fa[u]=v;
                if(++cnt>=n-1)return i;
            }
        }
        return -1;
    }
}kk;
int main()
{
    int n,m;
    int u,v,dis;
    cin>>n>>m;
    kk.intn(n);
    while(m--)
    {
        cin>>u>>v>>dis;
        kk.addedge(u,v,dis);
    }
  int num=kk. Kruskal();
  cout<<kk.edges[num].dis<<endl;
   cout<<n-1<<endl;
    for(int i=0;i<n-1;i++)
    {
        int id=kk.e[i];
        cout<<kk.edges[id].u<<" "<<kk.edges[id].v<<endl;
    }
return 0;
}