POJ 1861 ——Network——————【最小瓶颈生成树】

Network

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15268   Accepted: 5987   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 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

题目大意:给你n个顶点m条无向边。让你求一棵生成树,使得最大边权尽量小。输出最长边,边的条数,哪些边。这题是特判,所以跟样例输出可能不太一样。

解题思路:即求最小瓶颈生成树。当用kruskal算法求解的时候。图第一次连通的时候,最后加入的那条边,即为所求。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 1010;
const int maxe = 15010;
struct Edge{
    int from,to,dist,idx;
    Edge(){}
    Edge(int _from,int _to,int _dist,int _idx):from(_from),to(_to),dist(_dist),idx(_idx){}
}edges[maxe];
struct Set{
    int pa,rela;
}sets[maxn];
int ans[maxn];
bool cmp(Edge a,Edge b){
    return a.dist < b.dist;
}
void init(int n){
    for(int i = 0; i <= n; i++){
        sets[i].pa =  i;
    }
}
int Find(int x){
    if(x == sets[x].pa){
        return x;
    }
    int tmp = sets[x].pa;
    sets[x].pa = Find(tmp);
    return sets[x].pa;
}
int main(){
    int n, m;
    while(scanf("%d%d",&n,&m)!=EOF){
        init(n);
        int a,b,c;
        for(int i = 0; i < m; i++){
            scanf("%d%d%d",&a,&b,&c);
            edges[i] = Edge(a,b,c,i);
        }
        sort(edges,edges+m,cmp);
        int cnt = 0;
        for(int i = 0; i < m; i++){
            Edge & e = edges[i];
            int rootx, rooty;
            rootx = Find(e.from);
            rooty = Find(e.to);
            if(rootx == rooty){
                continue;
            }
            sets[rooty].pa = rootx;
            ans[cnt++] = i;
        }
        printf("%d\n",edges[ans[cnt-1]].dist);
        printf("%d\n",cnt);
        for(int i = 0; i < cnt; i++){
            printf("%d %d\n",edges[ans[i]].from,edges[ans[i]].to);
        }
    }
    return 0;
}

  

时间: 2024-10-05 23:58:42

POJ 1861 ——Network——————【最小瓶颈生成树】的相关文章

poj 1861 Network

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13260   Accepted: 5119   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 c

poj Command Network 最小树形图

规定根节点,求一颗生成树使得权值最小,但由于是有向图,所以最小生成树算法失效. 查资料后得知此类问题叫做最小树形图. 解决最小树形图问题的朱刘算法,算法核心基于找 [最小弧集->找环,消环缩点] 的思想,来慢慢构造树形图. 所有的灵魂都在这张图上.0.0 注意缩点后的弧权值的处理 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algo

poj 1861 Network (kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13633   Accepted: 5288   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 c

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 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, t

【最小瓶颈生成树】【最小生成树】【kruscal】bzoj1083 [SCOI2005]繁忙的都市

本意是求最小瓶颈生成树,但是我们可以证明:最小生成树也是最小瓶颈生成树(其实我不会).数据范围很小,暴力kruscal即可. 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 struct Edge{int u,v,w;void Read(){scanf("%d%d%d",&u,&v,&w);}}edges[10001]; 5 bool operator &

(最小生成树/最小瓶颈生成树) 2017武汉现场赛 - Wifi Relay

题意: n个无线AP,有xy坐标属性,现在n个无线AP要桥接在一起不能断开连接,现在要求无线AP无线网络的覆盖半径最小是多少 分析: 看起来是像是最小生成树,这里是是求生成树中最长的边最短,就是最小瓶颈生成树. 可以证明最小瓶颈生成树就是最小生成树,详细看刘汝佳<算法入门经典训练指南>343页. 当时现场的时候,想试试最小生成树了,结果以为n方复杂度过不去,就没写,现在想起来,真是坑哦. 这题n有10000,所以不能直接建邻接矩阵,而是采用动态计算距离就行了. 比赛结束了随便一写就A了...

poj 1861 Network 解题报告

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16171   Accepted: 6417   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 c

POJ 1861 Network (MST)

题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 #define INF 0x7f7f7f7f 7 #define pii pair<int,

BZOJ 3732 Network 最小瓶颈路

题目大意:给出一个无向边,很多询问,问x,y两地之间的最长路最短是多少. 思路:乍一看好像是二分啊.的确这个题二分可以做,但是时间会慢很多,有的题直接就T掉(NOIP2013货车运输).其实这个题的模型就是最小瓶颈路模型.解法就是把无向图变成一个最小生成树,然后两点之间的最长路就是满足题意的答案. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm&g