POJ1861 Network(Kruskal)(并查集)

Network

Time Limit: 1000MS

    Memory Limit: 30000K
Total Submissions: 16047   Accepted: 6362   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【分析】首先,这一题有问题。第一,输入文件包含多个测试用据,他没说;第二,测试用例的结果错了,应该是131 21 33 4而且应该是多判的,可以用Kruskal;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=1005;
const int M=15005;
vector<int>q;
struct Edg {
    int v,u;
    int w;
} edg[M];
bool cmp(Edg g,Edg h) {
    return g.w<h.w;
}
int n,m,k,maxn;
int parent[N];
void init() {
    for(int i=0; i<n; i++)parent[i]=i;
}
void Build() {
    int u,v,w;
    for(int i=0; i<m; i++) {
        scanf("%d%d%d",&u,&v,&w);
        edg[i].u=u;
        edg[i].v=v;
        edg[i].w=w;
    }
    sort(edg,edg+m,cmp);
}
int Find(int x) {
    if(parent[x] != x) parent[x] = Find(parent[x]);
    return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    parent[y] = x;
}//将两个不同集合的元素进行合并
void Kruskal() {
    int sum=0;
    int num=0;
    int u,v;
    for(int i=0; i<m; i++) {
        u=edg[i].u;
        v=edg[i].v;
        if(Find(u)!=Find(v)) {
            sum+=edg[i].w;
            maxn=max(maxn,edg[i].w);
            q.push_back(i);
            num++;
            Union(u,v);
        }
        if(num>=n-1) {
            printf("%d\n%d\n",maxn,n-1);

            break;
        }
    }
}
int main() {
    while(~scanf("%d%d",&n,&m)) {
        while(!q.empty())q.pop_back();
        maxn=-1;
        init();
        Build();
        Kruskal();
        for(int i=0; i<q.size(); i++) {
            int l=q[i];
            printf("%d %d\n",edg[l].u,edg[l].v);
        }
    }
    return 0;
}

时间: 2024-10-14 17:45:30

POJ1861 Network(Kruskal)(并查集)的相关文章

SDUT 2933-人活着系列之Streetlights(最小生成树Kruskal+并查集实现)

人活着系列之Streetlights Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 人活着如果是为了家庭,亲情----可以说是在这个世界上最温暖人心的,也是最让人放不下的,也是我在思索这个问题最说服自己接受的答案.对,也许活着是一种责任,为了繁殖下一代,为了孝敬父母,男人要养家糊口,女人要生儿育女,就这样循环的过下去,但最终呢?还是劳苦愁烦,转眼成空呀! 为了响应政府节约能源的政策,某市要对路灯进行改革,已知该市有n个城镇,

Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0.然后进行一遍prim(2)Kruskal算法+并查集 代码: //prim写法 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

poj 2236:Wireless Network(并查集,提高题)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 6778 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

codevs 1078最小生成树 Kruskal+并查集

题目描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场.为了使花费最少,他想铺设最短的光纤去连接所有的农场. 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案. 每两个农场间的距离不会超过100000 输入描述 Input Description 第一行: 农场的个数,N(3<=N<=100).

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

POJ2236 Wireless Network 【并查集】

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16885   Accepted: 7091 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

最小生成树(kruskal+并查集)

最小生成树 最小生成树即用最少的边权将所有给定的点连在同一联通分量中,常用kruskal和prim算法 kruskal算法(适合稀疏图) 最小生成树的kruskal算法,稍带并查集的应用 int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); //不要漏了fa[x]=... } int kruskal() { int ans=0; for(int i=0;i<N;i++) fa[i]=i;//初始化并查集 sort(edge,edge+e);