POJ 1861 NetWork (Kruskal算法)
2011-09-14 15:20
393 查看
Network
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
Sample Output
Source
Northeastern Europe 2001, Northern Subregion
//kruskal算法
题意:找出最小生成树的最大边,然后再输出最小生树的连结的边的个数和边(注:样例有误)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 15005
#define swap(x, y) {int t = x; x = y; y = t;}
struct point {
int u, v, w, f;
}pnt
;
int rank
, set
;
bool cmp_1(struct point p1, struct point p2) {
return p1.w < p2.w;
}
bool cmp_2(struct point p1, struct point p2) {
if(p1.f != p2.f) return p1.f > p2.f;
else {
if(p1.u != p2.u) return p1.u < p2.u;
else return p1.v < p2.v;
}
}
void make_set(int n) {
for(int i = 0; i <= n; i++) {
rank[i] = 1;
set[i] = i;
}
}
int find_set(int x) {
if(x != set[x]) return set[x] = find_set(set[x]);
return set[x];
}
void merge(int x, int y) {
int xx = find_set(x);
int yy = find_set(y);
if(xx == yy) return;
if(rank[xx] > rank[yy]) {
set[yy] = xx;
rank[xx] += rank[yy];
} else {
set[xx] = yy;
rank[yy] += rank[xx];
}
}
int main()
{
int i, cnt, m, n, min;
while(scanf("%d%d", &m, &n) != EOF) {
make_set(m);
for(i = 0; i < n; i++) {
scanf("%d%d%d",&pnt[i].u, &pnt[i].v, &pnt[i].w);
pnt[i].f = 0;
if(pnt[i].u > pnt[i].v) swap(pnt[i].u, pnt[i].v);
}
sort(pnt, pnt+n, cmp_1);
min = 0; cnt = 0;
for(i = 0; i < n; i++) {
if(find_set(pnt[i].u) != find_set(pnt[i].v)) {
if(min < pnt[i].w) min = pnt[i].w;
pnt[i].f = 1;
cnt += 1;
merge(pnt[i].u, pnt[i].v);
}
}
sort(pnt, pnt+n, cmp_2);
printf("%d\n", min);
printf("%d\n", cnt);
for(i = 0; i < n && pnt[i].f; i++) {
printf("%d %d\n", pnt[i].u, pnt[i].v);
}
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 9192 | Accepted: 3396 | 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 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
Source
Northeastern Europe 2001, Northern Subregion
//kruskal算法
题意:找出最小生成树的最大边,然后再输出最小生树的连结的边的个数和边(注:样例有误)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 15005
#define swap(x, y) {int t = x; x = y; y = t;}
struct point {
int u, v, w, f;
}pnt
;
int rank
, set
;
bool cmp_1(struct point p1, struct point p2) {
return p1.w < p2.w;
}
bool cmp_2(struct point p1, struct point p2) {
if(p1.f != p2.f) return p1.f > p2.f;
else {
if(p1.u != p2.u) return p1.u < p2.u;
else return p1.v < p2.v;
}
}
void make_set(int n) {
for(int i = 0; i <= n; i++) {
rank[i] = 1;
set[i] = i;
}
}
int find_set(int x) {
if(x != set[x]) return set[x] = find_set(set[x]);
return set[x];
}
void merge(int x, int y) {
int xx = find_set(x);
int yy = find_set(y);
if(xx == yy) return;
if(rank[xx] > rank[yy]) {
set[yy] = xx;
rank[xx] += rank[yy];
} else {
set[xx] = yy;
rank[yy] += rank[xx];
}
}
int main()
{
int i, cnt, m, n, min;
while(scanf("%d%d", &m, &n) != EOF) {
make_set(m);
for(i = 0; i < n; i++) {
scanf("%d%d%d",&pnt[i].u, &pnt[i].v, &pnt[i].w);
pnt[i].f = 0;
if(pnt[i].u > pnt[i].v) swap(pnt[i].u, pnt[i].v);
}
sort(pnt, pnt+n, cmp_1);
min = 0; cnt = 0;
for(i = 0; i < n; i++) {
if(find_set(pnt[i].u) != find_set(pnt[i].v)) {
if(min < pnt[i].w) min = pnt[i].w;
pnt[i].f = 1;
cnt += 1;
merge(pnt[i].u, pnt[i].v);
}
}
sort(pnt, pnt+n, cmp_2);
printf("%d\n", min);
printf("%d\n", cnt);
for(i = 0; i < n && pnt[i].f; i++) {
printf("%d %d\n", pnt[i].u, pnt[i].v);
}
}
return 0;
}
相关文章推荐
- POJ 1861 Network (Kruskal算法)
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- POJ 1861 Network (模版kruskal算法)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- Pku acm 1861 NetWork 数据结构题目解题报告(二) ----最小生成树:prim算法&amp;amp;Kruskal算法
- poj 1861 Network(并查集)
- poj 1861 Network
- POJ-1861-NETWORK 解题报告
- POJ 1861 Network 最小生成树
- poj1861最小生成树(并查集)-kruskal算法
- POJ 1861 Network (Kruskal+并查集)
- POJ 1861 Network
- POJ1861 Network
- poj 1861 network
- POJ 1861 Network【最小生成树】
- POJ1861-Network(Kruskal)
- POJ 1861 Network (最短路)
- POJ-1861-Network
- poj 1861 Network 解题报告