POJ 1258(最小生成树+知识)
用kruskal算法,利用w[i]给r[i]间接排序,从而r[i]可以按照边大小保存序号,同时要判断是否在一个集合里面
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std; #define sf scanf
#define pf printf
#define debug printf("!\n")
#define blank printf("\n")
#define mem(a,b) memset(a,b,sizeof(a)) const int MaxN = ;
const int INF = <<; int p[MaxN]; int map[MaxN][MaxN],w[MaxN*MaxN],r[MaxN*MaxN],u[MaxN*MaxN],v[MaxN*MaxN]; int m,n; int find(int x){return p[x]==x?x:p[x]=find(p[x]);} int cmp(const int a,const int b)
{
return w[a]<w[b];
} int kruskal()
{
int ans = ,i; for(i = ;i<n;i++) p[i] = i;
for(i = ;i<m;i++) r[i] = i; sort(r,r+m,cmp); for(i = ;i<m;i++)
{
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if(x!=y)
{
ans+=w[e];p[x] = y;
}
}
return ans;
} int main()
{
int i,j;
while(~sf("%d",&n))
{
m = ;
mem(u,);
mem(v,);
mem(w,);
for(i = ;i<n;i++)
{
for(j=;j<n;j++)
sf("%d",&map[i][j]);
}
for(i = ;i<n;i++)
{
for(j=i+;j<n;j++)
{
u[m] = i;
v[m] = j;
w[m++] = map[i][j];
}
}
int ans = kruskal();
pf("%d\n",ans); } return ;
}
POJ 1258(最小生成树+知识)的更多相关文章
- poj 1258 最小生成树 模板
POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...
- POJ 1258 最小生成树
23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求 ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- poj - 1258 Agri-Net (最小生成树)
http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...
- (最小生成树)Agri-Net -- POJ -- 1258
链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- poj 1258 Agri-Net 最小生成树 kruskal
点击打开链接 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33733 Accepted: 13539 ...
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
随机推荐
- mylyn提交到JIRA的日期格式错误
HTTP Status 400 - Date value '27/Dec/11' for field 'due' is invalid. Valid formats include: 'yyyy/MM ...
- ionic 项目 随笔
1,首先 会进入src/index.html, <!-- The polyfills js is generated during the build process --> <sc ...
- AngularJS源码解析2:注入器的详解
上一课,没有讲createInjector方法,只是讲了它的主要作用,这一课,详细来讲一下这个方法.此方法,最终返回的注册器实例对象有以下几个方法: invoke, instantiate, get, ...
- iOS学习笔记(7)——解析json中的中文
NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"]; NSError *error = ni ...
- P1975 [国家集训队]排队
题目链接 题意分析 我们考虑 交换两个数\([le,ri]\)的贡献 减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数 增加的\([le,ri]\)中 ...
- leetcode-268-Missing Number(异或)
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- L02-RHEL6.5环境中安装JDK1.8
注: 1.本文安装的是jdk1.8,采用rpm包的方式安装. 2.rpm安装方式默认会把jdk安装到/usr/java/jdk1.8xxx 路径上,若想将JDK安装到特定路径,需以源码方式安装,可参考 ...
- 【性能测试】:监控Mysql数据库方式
1, 进入到/etc目录下,打开my.cnf文件,在文件最后添加几行 slow_query_log = ON //打开慢查询开关 slow_q ...
- 南昌 Max answer
https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a interva ...
- js实现瀑布流布局
window.onload = function () { var d1 = new Waterfall(); d1.init();};//构造函数function Waterfall() { thi ...