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 ...
随机推荐
- 关于yield创建协程的理解
先上利于理解的代码: #coding:utf-8 def consumer(): c_r = '' while 1: m = yield c_r if not m: return print(&quo ...
- Linux必学的60个命令【转载】
Linux提供了大量的命令,利用它可以有效地完成大量的工 作,如磁盘操作.文件存 [转载地址]http://blog.chinaunix.net/uid-16728139-id-3154272.ht ...
- EventStore的设计思路
EventStore的设计思路 最近打算用C#实现一个基于文件的EventStore. 什么是EventStore 关于什么是EventStore,如果还不清楚的朋友可以去了解下CQRS/Event ...
- Python CSV文件处理/读写及With as 用法
可以不使用CSV模块 逐行处理: for line in open("samples/sample.csv"): title, year, director = line.spli ...
- BeanFactory 和ApplicationContext
BeanFactory和ApplicationContext对待bean后置处理器稍有不同.ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有 ...
- PhoneGap 3.0 安装
PhoneGap 3.0 已经出来有一段时间了.3.0 提供了使用Node.js 安装,使用命令行创建.编译.运行项目.也就是可以抛弃eclipse,完全使用命令.记事本开发phonegap 项目了 ...
- Flex 全屏显示方法
1,修改html-template下的index.template.html文件…增加四行 1</html> 上述文件增加了四行…见我文中有提示 2,Mxml文件: 假如一个button按 ...
- 对象图(Object Diagram)—UML图(三)
一.用一张图来介绍一下对象图的基本内容 二.对象图与类图的基本差别 三.对象图实例
- CSS3属性之border-radius
一.语法: 代码如下: border-radius : none | <length>{1,4} [/ <length>{1,4} ]? 二.取值: <length& ...
- input autocomplete 下拉提示+支持中文
js 代码: $.getJSON("/Foreign/Getforeign_routeEndPoint", function (data) { $(" ...