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 ...
随机推荐
- codeforces 400E. Inna and Binary Logic 线段树
题目链接 给出n个数, 定义a[1][i]为这初始的n个数, 然后a[i][j] = a[i-1][j]&a[i-1][j-1], 这样就可以得到一个三角形一共n*(n-1)/2个数. 给出一 ...
- GPL协议
GPL-General Public License 它是自由软件许可(Free Software Licence),保证最终使用者能够自由的引用,学习,拷贝,甚至修改软件.在理解这之前需要了解什么是 ...
- pcap文件格式解析
pcap文件格式是常用的数据报存储格式,包括wireshark在内的主流抓包软件都可以生成这种格式的数据包 下面对这种格式的文件简单分析一下: pcap文件的格式为: 文件头 24字节 ...
- Ajax 生成流文件下载 以及复选框的实现
JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...
- Oracle SQL篇(二)oracle自连接操作
oracle 的自连接(self join)操作 对于oracle数据库来说,目前支持两套语法,一套是oracle自己的sql语法,一套是通行标准的SQL99语法,那么对于oracle的连接操 ...
- HDU 1498 50 years, 50 colors
题目大意:给你一个 n*n 的矩阵,每个格子上对应着相应颜色的气球,每次你可以选择一行或一列的同种颜色的气球进行踩破,问你在K次这样的操作后,哪些颜色的气球是不可能被踩破完的. 题解:对于每一种颜色建 ...
- java学习之坦克大战游戏
总结:由于这几天快过年比较忙然后没怎么写,写代码途中一些经验总结现在给忘记了.这次的小项目感觉比上次写的思路清楚了点.没有之前第一次写那么逻辑混乱,结构也搞的比之前的要好,添加功能比较容易.学习了之前 ...
- Android清单文件具体解释(三)----应用程序的根节点<application>
<application>节点是AndroidManifest.xml文件里必须持有的一个节点,它包括在<manifest>节点下.通过<application>节 ...
- 详解iOS开发之自定义View
iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...
- Windows Server 2012 安装dll到GAC
使用Windows管理员打开PowerShell: 运行以下命令: Set-location "c:\tools\gac" [System.Reflection.Assembly] ...