您的位置:首页 > 其它

ZOJ 1542 poj 1861 Network(并查集+最小树)

2013-02-14 16:15 369 查看
Network

Time Limit: 2 Seconds
Memory Limit: 65536 KB Special Judge

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

Process to the end of file.

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

Source: Northeastern Europe 2001, Northern Subregion

Submit Status

Copyright @

思路:这题实际上就是最小树,因为最小树里了最长边,就是题目中所述的最短边。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
const int mm=1010;
const int nn=15009;
int root[mm],ran[mm];
struct node{int u,v,c;}edge[nn];
int look(int x)
{
if(x^root[x])root[x]=look(root[x]);
return root[x];
}
void uni(int x,int y)
{
if(ran[x]>ran[y])root[look(y)]=look(x),ran[x]+=ran[y];
else root[look(x)]=look(y),ran[y]+=ran[x];
}
bool cmp(node a,node b)
{
return a.c<b.c;
}
int n,m;
int main()
{
while(cin>>n>>m)
{
for(int i=0;i<m;i++)
cin>>edge[i].u>>edge[i].v>>edge[i].c;
sort(edge,edge+m,cmp);
int len,num=0,top=-1;
for(int i=0;i<=n;i++)root[i]=i,ran[i]=1;
for(int i=0;i<m;i++)
if(look(edge[i].u)^look(edge[i].v))///top跳跃指针存路径
uni(edge[i].u,edge[i].v),len=edge[i].c,++num,edge[i].c=top,top=i;
cout<<len<<"\n"<<num<<"\n";
while(top!=-1)
{
cout<<edge[top].u<<" "<<edge[top].v<<"\n";top=edge[top].c;
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: