用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(最小生成树+知识)的更多相关文章

  1. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  2. POJ 1258 最小生成树

    23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求 ...

  3. 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 ...

  4. 最小生成树 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 // 顶点的最大个数 ( ...

  5. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  6. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

  7. (最小生成树)Agri-Net -- POJ -- 1258

    链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  8. poj 1258 Agri-Net 最小生成树 kruskal

    点击打开链接 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33733   Accepted: 13539 ...

  9. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

随机推荐

  1. objectARX 关于MFC类向导 无法向此非CCmdTarget派生类添加任何命令 的解决方式

    objectARX 关于MFC类向导 无法向此非CCmdTarget派生类添加任何命令  的解决方式 图文By edata ,转载注明出处 http://www.cnblogs.com/edata 1 ...

  2. 3. STL编程三

    1. 算法容器的使用: #include <iostream> #include <functional> #include <vector> #include & ...

  3. Spring注解大全,汇总版

    Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...

  4. MySQL与Redis实现二级缓存

    redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化, ...

  5. CF1139E Maximize Mex 题解【二分图】

    我发现我有道叫[SCOI2010]连续攻击游戏的题白写了.. Description There are \(n\) students and \(m\) clubs in a college. Th ...

  6. 剑指offer——面试题30:包含min函数的栈

    #include"iostream" #include"stdio.h" using namespace std; ; ; template<typena ...

  7. 【Maven学习】maven-assembly-plugin的使用

    转自http://liugang594.iteye.com/blog/2093607 maven-assembly-plugin使用描述(拷自 maven-assembly-plugin 主页) Th ...

  8. ubuntu 添加多个IP

    上次这个问题 :  UBUNTU 无法解析域名 解决方法 解决后,每次重启电脑都需要重新配置 /etc/resolv.conf. 这次添加IP的时候留心发现,在/etc/network/interfa ...

  9. 初始设置ubuntu 16.04 Vps部署rails

    参考 https://blog.longwin.com.tw/2005/12/ssh_keygen_no_passwd/ 1 选择搬瓦工左边菜单栏中的“Root password modificati ...

  10. Redis之数据类型Sting字符串

    Redis String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value. string类型是二进制安全的.意思是redi ...