Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: Accepted: 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 ( <= N <= ) and M - the number of possible hub connections ( <= M <= ). All hubs are numbered from 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 . 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

View Question

代码WA了,待查找原因

 #include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 1000
int father[MAX], son[MAX], Min=0x3fffffff;
int v, l; typedef struct Kruskal //存储边的信息
{
int a;
int b;
int value;
}; bool cmp(const Kruskal & a, const Kruskal & b)
{
return a.value < b.value;
} int unionsearch(int x) //查找根结点+路径压缩
{
return x == father[x] ? x : unionsearch(father[x]);
} bool join(int x, int y) //合并
{
int root1, root2;
root1 = unionsearch(x);
root2 = unionsearch(y);
if(root1 == root2) //为环
return false;
else if(son[root1] >= son[root2])
{
father[root2] = root1;
son[root1] += son[root2];
}
else
{
father[root1] = root2;
son[root2] += son[root1];
}
return true;
} int main()
{
int ltotal;
int res_f[],res_b[];
Kruskal edge[MAX];
while(scanf("%d%d",&v,&l)!=EOF)
{
ltotal = ;
for(int i = ; i <= v; ++i) //初始化
{
father[i] = i;
son[i] = ;
}
for(int i = ; i <= l ; ++i)
{
scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].value);
}
sort(edge + , edge + + l, cmp); //按权值由小到大排序
for(int i = ; i <= l; ++i)
{
if(join(edge[i].a, edge[i].b))
{
res_f[ltotal]=edge[i].a; res_b[ltotal]=edge[i].b;
ltotal++; //边数加1
//cout<<edge[i].a<<" "<<edge[i].b<<endl;
if(edge[i].value < Min)
Min=edge[i].value;
}
}
printf("%d\n%d\n",Min,ltotal);
for(int i=;i<ltotal;i++){
printf("%d %d\n",res_f[i],res_b[i]);
}
}
return ;
}

克鲁斯卡尔(Kruskal)算法(只与边相关)

算法描述:克鲁斯卡尔算法需要对图的边进行访问,所以克鲁斯卡尔算法的时间复杂度只和边又关系,可以证明其时间复杂度为O(eloge)。

算法过程:

1.将图各边按照权值进行排序

2.将图遍历一次,找出权值最小的边,(条件:此次找出的边不能和已加入最小生成树集合的边构成环),若符合条件,则加入最小生成树的集合中。不符合条件则继续遍历图,寻找下一个最小权值的边。

3.递归重复步骤1,直到找出n-1条边为止(设图有n个结点,则最小生成树的边数应为n-1条),算法结束。得到的就是此图的最小生成树。

克鲁斯卡尔(Kruskal)算法因为只与边相关,则适合求稀疏图的最小生成树。而prime算法因为只与顶点有关,所以适合求稠密图的最小生成树。

摘自http://blog.csdn.net/niushuai666/article/details/6689285

AC代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <malloc.h>
#include <algorithm>
#define MAX 10500
#define INF 0x3FFFFFFF
using namespace std;
int par[MAX],n,m,maxedge,cnt;
struct Edge{
int s,e;
int value;
}edge[MAX],index[MAX]; bool cmp(Edge a, Edge b){
return a.value < b.value;
} int find(int x){
while(par[x] != x)
x = par[x];
return x;
} void connect(int a,int b){
if(a < b)
par[b] = a;
else
par[a] = b;
} void kruskal(){
int i,j;
maxedge = ;
cnt = ;
for(i=; i<=m; i++)
{
int a = find(edge[i].s);
int b = find(edge[i].e);
if(a != b)
{
connect(a,b);
if(maxedge < edge[i].value);
maxedge = edge[i].value;
cnt ++;
index[cnt].s = edge[i].s;
index[cnt].e = edge[i].e;
}
if(cnt >= n-)
break;
}
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m) != EOF){
for(i=; i<=m; i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].value);
} sort(edge+,edge++m,cmp); for(i=; i<=n; i++){
par[i] = i;
}
memset(index,,sizeof(index));
kruskal();
printf("%d\n%d\n",maxedge,cnt); for(i=; i<=cnt; i++){
printf("%d %d\n",index[i].s,index[i].e);
}
}
return ;
}

POJ 1861 Network (模版kruskal算法)的更多相关文章

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

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  2. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  3. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

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

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15268   Accepted: 5987   Specia ...

  5. POJ 1861 Network

    题意:有n个点,部分点之间可以连接无向边,每条可以连接的边都有一个权值.求一种连接方法将这些点连接成一个连通图,且所有连接了的边中权值最大的边权值最小. 解法:水题,直接用Kruskal算法做一遍就行 ...

  6. POJ 2421 Constructing Roads(Kruskal算法)

    题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...

  7. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  8. POJ 1861 Network (Kruskal求MST模板题)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Specia ...

  9. POJ 1861 Network (MST)

    题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...

随机推荐

  1. Protel99se教程七:创建PCB元件封装

    在上一节课当中,我们给大家讲解了如何制作SCH原理图的元件库,这一节课,我们给大家讲解的是如何制作protel99se封装,在我们制作好元件好,需要制作对应的封装库,以供PCB设计所用. 第一步:进入 ...

  2. 盛希泰:办公室就像男人的春药——人的一生的精力是有限的,你把有限的时间分配给谁决定你的成败——你有N多选择,你人生的积累就是N多选择加起来的结果

    欢迎关注“创事记”的微信订阅号:sinachuangshiji 创事记注:12月22日晚上,盛希泰在清华大学旧经管报告厅面对清华师生讲了一堂<创业引导课>.本文由洪泰帮根据课堂录音整理完成 ...

  3. Objective-C浅拷贝和深拷贝

    浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间 如: char* str = (char*)malloc(100);char* str2 = str; 浅拷贝只是对对象的简单拷贝 ...

  4. xen虚拟机操作整理

    1,登陆物理机器 2,查看物理机建立虚拟机的列表 root:~ # xm li Name ID Mem VCPUs State Time(s) Domain-0 0 49450 8 r----- 52 ...

  5. 讲讲金融业务(一)--自助结算终端POS

    之前在群里和大家聊天的时候,发现好多人对银行业务比較感兴趣,或许是由于大家对银行不了解,以为非常神奇的样子.全部,从这周開始我打算把我肚子里的墨水慢慢地倒出来,和大家分享分享.   在技术还不发达的时 ...

  6. [学习笔记]jQuery实现一些动画效果的方法

    jQuery实现效果的方法 1,隐藏和显示:hide(),show(),toggle()  // [ˈtɑ:gl]切换 语法: $(selector).hide(speed,callback); $( ...

  7. 【转】KVM/Installation

    [转]KVM/Installation Installation Pre-installation checklist Check that your CPU supports hardware vi ...

  8. HtmlUnit+Jsoup 解决爬虫无法解析执行javascript的问题

    本人最近在研究爬虫.作为一个新手.研究了些爬虫框架,发现所有开源的爬虫框架很多,功能也很齐全,但唯独遗憾的是,目前还没有发现那个爬虫对js完美的解释并执行.看了浅谈网络爬虫爬js动态加载网页(二)之后 ...

  9. 解决了clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决方法

    1.”Build Settings”->”Enable Bitcode”设置为NO 2.TARGETS -->  Build Settings --> Architectures - ...

  10. Android 开发笔记 “Android 的消息队列模型”

    Android是参考Windows的消息循环机制来实现Android自身的消息循环的. Android通过Looper.Handler来实现消息循环机制,Android消息循环是针对线程的(每个线程都 ...