POJ 1861 Network (模版kruskal算法)
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算法)的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- ZOJ 1586 QS Network(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- POJ 1861 ——Network——————【最小瓶颈生成树】
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15268 Accepted: 5987 Specia ...
- POJ 1861 Network
题意:有n个点,部分点之间可以连接无向边,每条可以连接的边都有一个权值.求一种连接方法将这些点连接成一个连通图,且所有连接了的边中权值最大的边权值最小. 解法:水题,直接用Kruskal算法做一遍就行 ...
- POJ 2421 Constructing Roads(Kruskal算法)
题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- POJ 1861 Network (MST)
题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...
随机推荐
- ios 学习笔记(8) 控件 按钮(UIButton)的使用方法
在实际开发中,对于开发者来说,更多的还是使用“自定义”按钮.将“按钮”对象的类型设置成UIButtonTypeCustom.这样一来,按钮的所有元素都将由开发者来配置和自定义. 对于一个自定义按钮来说 ...
- Nexus 5完全拆解
Nexus 5,由LG制造,配备高通骁龙四核处理器,4.95英寸1080P显示屏,支持4G LTE,运行最新的Android 4.4 KitKat原生操作系统.国外著名拆解网站iFixit第一时间带来 ...
- Sicily-1028
一. 题意: 算出汉诺塔移动序列中对应位置的号码,数据规模很大,所以不能单纯递归,而是要找出汉诺塔序列的规律. 二. 汉诺塔数列 为了得出最少的移动步数,当n为偶数时,最上 ...
- [Python]Unicode转ascii码的一个好方法
写这篇文章的是一位外国人,他遇到了什么问题呢?比如有一个 Unicode 字符串他需要转为 ascii码: >>> title = u"Klüft skräms inför ...
- Qt5 FOR WINCE7, Visual Studio 2008环境的搭建
Qt5 FOR WINCE7, Visual Studio 2008环境的搭建 Qt5发布时,试过配置Qt5 for wince的环境,原因是暂时不支持WINCE.前几天意外发现官方博客说明已经开始支 ...
- 细数C++和C的差别
C++语言是对C语言的扩展.所以熟悉C语言的人会发现.本书的第01~18章讲的内容基本上和C语言的内容差点儿相同. C++一方面对C语言的语法进行了改动.还有一方面又加入一些新的概念. C++中新增的 ...
- hadoop高速扫盲帖,从零了解hadoop
1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...
- 更新Windows Azure Web Site中的Orchard版本
官方建议大家使用本地副本来更新 1.首先做个全站备份,这样更新好以后出问题你就很容易回滚 . Web Site 做备份很方便.把网站SCALE设置到STANDARD,然后在BACKUPS页面里面点备份 ...
- Unqualified name lookup
Unqualified name lookup File scope Namespace scope For an qualified name, that is a name that does n ...
- [置顶] cocos2d-x 植物大战僵尸(13)类似酷跑的【同一角色不同动画间的切换的实现】
有几天没和大家分享博客了,原因很简单,就是我在运行第12章所写的代码时:(开始一切正常,不过没多久就出现了内存泄露!.可能求成心切吧,当时没多加考虑就把代码发上去了.我在此对看过第12章得 ...