Network
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 5438 |
|
Accepted: 2115 |
|
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 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.
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
#include
#define MAX 15001
/* 定义边(x,y),权为w */
typedef struct
{
int x, y;
int w;
}edge;
edge e[MAX];
edge v[MAX];
/* rank[x]表示x的秩 */
int rank[MAX];
/* father[x]表示x的父节点 */
int father[MAX];
/* 比较函数,按权值非降序排序 */
int cmp(const void *a, const void *b)
{
return (*(edge *)a).w - (*(edge *)b).w;
}
/* 初始化集合 */
void Make_Set(int x)
{
father[x] = x;
rank[x] = 0;
}
/* 查找x元素所在的集合,回溯时压缩路径 */
int Find_Set(int x)
{
if (x != father[x])
{
father[x] = Find_Set(father[x]);
}
return father[x];
}
/* 合并x,y所在的集合 */
void Union(int x, int y)
{
if (x == y) return;
if (rank[x] > rank[y])
{
father[y] = x;
}
else
{
if (rank[x] == rank[y])
{
rank[y]++;
}
father[x] = y;
}
}
int main()
{
int i, j, m, n, k, max;
int x, y;
scanf("%d%d", &m, &n);
/* 读入边数据 */
for (i = 0; i < n; i++)
{
scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w);
}
/* 初始化集合 */
for (i = 0; i < m; i++)
{
Make_Set(i);
}
/* 对边排序 */
qsort(e, n, sizeof(edge), cmp);
k = 0;
max = 0;
for (i = 0; i < n; i++)
{
x = Find_Set(e[i].x);
y = Find_Set(e[i].y);
if (x != y)
{
k++;
Union(x, y);
/* 保存边信息 */
v[k] = e[i];
/* 记录最大权值 */
if (max < e[i].w) max = e[i].w;
}
}
/* 输出结果 */
printf("%d/n", max);
printf("%d/n", k);
for (i = 1; i <= k; i++)
{
printf("%d %d/n", v[i].x, v[i].y);
}
//system("pause");
return 0;
}