Network
Time Limit: 1000MS | Memory Limit: 30000K | |||
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 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 5 8 1 2 5 1 4 2 1 5 1 2 3 6 2 4 3 3 4 5 3 5 4 4 5 6
Sample Output
1 4 1 2 1 3 2 3 3 4 4 4 1 5 1 4 2 4 3 5
————————————————————集训8.3的分割线————————————————————
前言:这道题原题的样例输出不是最优的(姑且这么说吧,总之是成环了),但是没有违背题意,毕竟题目只要求最长的那根最短,没有要求不成环、使用的线最少。
思路:完全可以当作Kruskal来做,因为Kruskal肯定是最小生成树。要在每次合并的时候做个记录,维护最长边,水题不解释。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 1005, M = 15005;
int n, m, ans, fa[N], maxD, line[N];
struct Node
{
int x, y, dis;
}edge[M];
bool cmp(Node a, Node b)
{
return a.dis < b.dis;
}
int Find(int x)
{
if(x != fa[x]) fa[x] = Find(fa[x]);
return fa[x];
}
void Union(int x, int y, int d, int id)
{
int fx = Find(x), fy = Find(y);
if(fx != fy) {
//printf("%d isn't in %d\n", y, x);
line[ans++] = id;
maxD = max(maxD, d);
fa[fy] = fx;
}
}
int main()
{
#ifdef J_Sure
freopen("1861.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(scanf("%d%d", &n, &m)!=EOF) {
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].dis);
}
sort(edge, edge+m, cmp);
maxD = -1;
ans = 0;
for(int i = 1; i <= n; i++)
fa[i] = i;
for(int i = 0; i < m; i++) {
Union(edge[i].x, edge[i].y, edge[i].dis, i);
}
printf("%d\n%d\n", maxD, ans);
int i;
for(i = 0; i < ans-1; i++)
printf("%d %d\n", edge[line[i]].x, edge[line[i]].y);
printf("%d %d\n", edge[line[i]].x, edge[line[i]].y);
}
return 0;
}