poj 1861 Network

来源:互联网 发布:星际争霸1数据 编辑:程序博客网 时间:2024/05/02 00:27
Network
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 17238 Accepted: 6926 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 61 2 11 3 11 4 22 3 13 4 12 4 1

Sample Output

141 21 32 33 4

Source

Northeastern Europe 2001, Northern Subregion

题意:求出一个使所有点都连通且最大边最小的生成树
思路:要使生成树中最大边最小,最简单的方法就是求出最小生成树就行(再加几条小于最大边的边也没问题,不过那样更麻烦),跑一遍最小生成树就行了,这里用的克鲁斯卡尔算法,好写一点

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define epp tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;int f[20000],r[20000],m,n,ans[20000][2];int Find(int x){    return f[x]==x?f[x]:f[x]=Find(f[x]);//并查集压缩路径}void join(int x,int y)//合并两个集合{    int x1=Find(x),y1=Find(y);    f[x1]=f[y1];}struct E{    int a,b,x;}e[20000];bool cmp(const E& a,const E& b)//按边的权值排序{    return a.x<b.x;}int main(){    while(~sf("%d%d",&m,&n))    {        int Max=0;//保存最大边        for1(i,m)            f[i]=i,r[i]=0;        for1(i,n)            sf("%d%d%d",&e[i].a,&e[i].b,&e[i].x);        sort(e+1,e+n+1,cmp);        int ed=0;        for(int i=1;i<=n&&ed<m-1;i++)        {            if(Find(e[i].a)!=Find(e[i].b))            {                join(e[i].a,e[i].b);                ans[ed][0]=e[i].a,ans[ed][1]=e[i].b;                ed++;                Max=max(Max,e[i].x);            }        }        pf("%d\n%d\n",Max,m-1);        for0(i,m-1)            pf("%d %d\n",ans[i][0],ans[i][1]);    }    return 0;}


原创粉丝点击