Network
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 16932 | Accepted: 6732 | 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.
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
最小生成树模板题,直接套就好了,克鲁斯卡尔的关键代码真是短啊。。。。。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stack>
#define max3(x, y, z) max((x), max((y), (z)))
#define min3(x, y, z) min((x), min((y), (z)))
#define pb push_back
#define mp make_pair
#define LL long long
using namespace std;
const int N = 100010;
struct edge
{
int u;
int v;
int w;
}edges[16000];
struct Node
{
int a;
int b;
}node[N];
int n, m;
int ne[N];
int maxx; ///生成树的最小边权值
int num; ///已选用的边的数目
bool cmp(edge a, edge b)
{
return a.w < b.w;
}
void ini()
{
for(int i = 1; i <= n; i ++){
ne[i] = i;;
}
}
int fi(int x)
{
int t = x;
while(t != ne[t]){
t = ne[t];
}
while(x != t){
int q = ne[x];
ne[x] = t;
x = q;
}
return t;
}
void join(int x, int y, int w)
{
int a = fi(x);
int b = fi(y);
//cout << sum << endl;
if(a != b){
node[num].a = x;
node[num].b = y;
num ++;
maxx = max(maxx, w);
ne[a] = b;
}
}
void kru()
{
int u, v, w; ///选用边的两个定点
for(int i = 0; i < m; i ++){
u = edges[i].u;
v = edges[i].v;
w = edges[i].w;
join(u, v, w);
if(num >= n - 1) break;
}
}
int main()
{
while(scanf("%d%d", &n, &m) == 2){
for(int i = 0; i < m; i ++){
scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].w);
}
sort(edges, edges + m, cmp);
maxx = 0;
num = 0;
ini();
kru();
printf("%d\n", maxx);
printf("%d\n", num);
for(int i = 0; i < num; i ++){
printf("%d %d\n", node[i].a, node[i].b);
}
}
return 0;
}