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 ...
随机推荐
- 求1+2+...+n
题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 程序很简单,就看想到想不到了.悲剧,我属于后者... 算 ...
- HDU 1983 Kaitou Kid - The Phantom Thief (2)
神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...
- DOC下编译和运行带有包的java类文件
前言: 带有包名的java类在DOC下编译可以成功,但是运行出错 错误: 找不到或无法加载主类 com.soanl.socket.MyServer D盘temp文件下有个Hello.java文件,包 ...
- Objective-c 协议(protocol)
协议的作用类似地C++中对抽象基类的多重继承.类似于Java中的接口(interface)的概念. 协议是多个类共享方法的列表,协议中列出的方法在本类中并没有相应实现,而是别的类来实现这些方法. ...
- Objective-c 类实现 (@implementation)
在用@interface声明类之后,可以使用@implementation进行实类的实现.类的实现的具体语法如下: @implementation 类名 方法实现代码; @end; 实例: @impl ...
- 关于python 模块导入
如何将自己写的库加入到python的库路径中: 首先查看python包含的库路径,步骤如下: a.打开python命令界面 b.import sys c.sys.path 1.在python安 ...
- 微信网页签名失败(invalid signature)
签名失败,建议按以下步骤排查 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验. 确认con ...
- 【server端学习】修改Apache配置使支持shtml
主要工作:修改httpd.conf文件[步骤一]去掉下面两行的注释#AddType text/html .shtml #AddOutputFilter INCLUDES .shtml [步骤二]在Op ...
- 利用jQuery获取数据,JSONP
最近工作用到了跨域请求,所以此文就有了,概念网上都有,就不细说了,直接来了. 看了一篇文章,说的是通过扩展让ASP.NET Web API支持JSONP,jsonp网上有很多的教程,js代码部分基本和 ...
- iOS开发之第三方登录微博-- 史上最全最新第三方登录微博方式实现
相关资源地址: 本项目demo地址 : https://github.com/zhonggaorong/weiboSDKDemo 最新SDK下载: 最新微博SDK 官网注册地址:点击打开链接 最新 ...