POJ 2421 Constructing Roads MST kruskal
最近刚学的并查集所以用kruskal来试试最小生成树~
kruskal其实用几句话就能说完~
1.贪心所有边的权值,从小到大取值
2.取值时~将边权非0的两个顶点~进行并查操作~如果两个点的祖先不同...边权加入最小生成树...并且将两个点纳入同一个集合中
3.判断是否所有点都在同一个集合中
完毕~
下面上代码~这个代码应该可以作为模版了...但是并查集没有优化~所以复杂度约为0(n^3)但是比prim好一点
32ms水过...
mian()前的代码修改一下可以作为kruskal的模版...我再写一篇专门放模版吧~
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int V = 101;
int father[V],map[V][V];
struct point
{int s,v,rank;
}p[V*V];
int cmp(point a, point b)
{ return a.rank<b.rank;
}
int find(int x)
{if(x!=father[x])father[x]=find(father[x]);return father[x];
}
void Union(int a,int b)
{int x = find(a);int y = find(b);if(x==y) return ;father[y]=x;
}
bool found(int n)
{int x=find(0);for(int i=0;i<n;i++)if(find(i)!=x)return false;return true;
}
int kruskal(int map[][V],int n)
{int i,j,cnt,mst=0;for(i=0,cnt=0;i<n;i++){father[i]=i;for(j=0;j<n;j++){p[cnt].s=i;p[cnt].v=j;p[cnt].rank = map[i][j];cnt++;}}sort(p,p+n*n,cmp);for(i=0;i<n*n;i++){if(p[i].rank!=0 && p[i].rank!=-1){if(find(p[i].s)!=find(p[i].v))mst+=p[i].rank;Union(p[i].s,p[i].v);if(found(n)==true)return mst;}else if(p[i].rank==-1){Union(p[i].s,p[i].v);if(found(n)==true)return mst;}}
}
int main()
{
// freopen("in.txt","r",stdin);int n;scanf("%d",&n);int m,i,j,p,q,cnt=0;memset(map,0,sizeof(map));for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&map[i][j]);cin>>m;while(m--){cin>>p>>q;map[p-1][q-1]=-1;}cout<<kruskal(map,n)<<endl; return 0;
}
转载于:https://www.cnblogs.com/Felix-F/archive/2012/08/10/3223657.html
POJ 2421 Constructing Roads MST kruskal相关推荐
- poj 2421 Constructing Roads
#include<iostream> //最小生成树primusing namespace std;const int max_ve=105,MaxWeight=2000;int n,ed ...
- 22.12.14补卡 Constructing Roads POJ - 2421
Constructing Roads - POJ 2421 - Virtual Judge 只有输入有点小坑 只有上三角的数据是有用的, 下三角的数据与上三角重复, 可不处理, 把i>=j的全部 ...
- POJ 1679 The Unique MST(次小生成树)
求次小生成树的两种方法. 第一种:Kruskal算法比较好操作.先求一遍最小生成树,然后再记录最小生成树上的边.然后再枚举删去最小生成树上的边,再求最小生成树,如果求出的最小生成树的花费等于第一次最小 ...
- HDU1102 Constructing Roads 最小生成树
点击打开链接 Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Ha ...
- 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)
题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得随意牛棚两个 ...
- HDOJ1102 Constructing Roads【最小生成树】-----武科大ACM暑期集训队选拔赛1题
这道题目没有做出来,代码写好之后一直没有AC,本以为做了这么多最小生成树的题目,这道题一定没问题的,结果很遗憾,没有注意细节问题: 首先,如何处理已经建好的路?已经建好的路说明这两个点是连通的,只要把 ...
- hdu 1025 Constructing Roads In JGShining's Kingdom(DP + 二分)
此博客为转发 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
最新文章
- 优秀程序员和普通程序员都是怎样过完一天的
- 图论 ---- E. Pairs of Pairs(构造+无向图的dfs树的性质)
- 全球项目多区域数据同步问题解决方案
- 中国万网域名注册量动态:12月上旬净增8424个
- android学习笔记33——资源ShapeDrawable
- Delphi控制Excel输出上标示例
- jQuery UI在Server 2008 IE8下DatePicker问题修复
- 一张图get jQuery所有方法
- 编程大讲坛、坛坛是佳酿--编程大讲坛:Visual Basic核心开发技术从入门到精通...
- 程序员修神之路--分布式系统设计理念这么难学?
- 『AA』AutoAnchor自动猫
- 一步一步写算法(之 回数)
- safari 浏览器提示添加到主屏幕_Safari浏览器的几个小技巧你掌握了吗?
- html个人简历网页
- python新浪微博爬虫_利用新浪API实现数据的抓取\微博数据爬取\微博爬虫
- java国际软件工程师_JAVA国际软件工程师--学生选课系统
- 11 个超火的前端必备在线工具,终于有时间上班摸鱼了
- 2021-01-01 中国公务员级别
- android刷脸支付宝,安卓手机可以支付宝刷脸吗 刷脸登陆安卓机能用吗
- 大厂是怎样对待线上故障的?