最小生成树

对于必须要加入的边, 让其边权为0 即可

Prim :

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 100 + 31;
const int INF=0x3f3f3f3f;
int Map[maxn][maxn];
int Vis[maxn], Dis[maxn];
int ans;
int n;
void Prim()
{
int k;
for(int i = 1; i <= n; ++i)
Dis[i] = Map[1][i], Vis[i] = 0;
Dis[1] = 0;
Vis[1] = 1;
for(int i = 1; i <= n; ++i)
{
int tmp = INF;
for(int j = 1; j <= n; ++j)
if(Vis[j] == 0 && tmp > Dis[j]) tmp = Dis[j], k = j;
if(tmp == INF) break;
Vis[k] = 1;
ans += Dis[k];
for(int j = 1; j <= n; ++j)
if(Vis[j] == 0 && Dis[j] > Map[k][j]) Dis[j] = Map[k][j];
}
} int main()
{
while(~scanf("%d",&n))
{
ans = 0;
//memset(Map,INF,sizeof(Map));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
scanf("%d",&Map[i][j]);
int q,a,b;
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&a,&b);
Map[a][b] = Map[b][a] = 0;
}
Prim();
printf("%d\n",ans);
}
return 0;
}

\

Kruskal :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 100 + 7;
struct Edges{
int u, v;
int val;
}edges[maxn * maxn];
int Pre[maxn];
int n,cnt,ans; void Add_Edge(int u,int v,int val)
{
edges[cnt].u = u, edges[cnt].v = v, edges[cnt].val = val;
cnt ++;
} void Init()
{
for(int i = 0; i < maxn ; ++i) Pre[i] = i;
cnt = 0;
ans = 0;
} int Find(int x)
{
int r = x;
while(r != Pre[r]) r = Pre[r];
return Pre[x] = r;
} bool Union(int x,int y)
{
int ax = Find(x), ay = Find(y);
if(ax == ay) return false;
Pre[ax] = ay;
return true;
} bool cmp(Edges a, Edges b)
{
return a.val < b.val;
} void Kruskal()
{
sort(edges,edges+cnt,cmp);
for(int i = 0; i < cnt; ++i)
{
if(Union(edges[i].u,edges[i].v)) ans += edges[i].val;
}
} int main()
{
int n;
while(~scanf("%d",&n))
{
Init();
int a, b, q;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
scanf("%d",&a);
Add_Edge(i,j,a);
}
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&a,&b);
a = Find(a);
b = Find(b);
Pre[a] = b;
}
Kruskal();
printf("%d\n",ans);
}
return 0;
}

第一道最小生成树,纪念一下。。

HDU 1102的更多相关文章

  1. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

  2. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  3. 图论问题(2) : hdu 1102

    题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是 ...

  4. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  5. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

  6. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  7. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  9. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  10. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 上传文件服务与web服务分离

    业务场景:1. 后端服务为java web应用,使用tomcat容器,多实例集群化部署.2. 前端使用nginx作为后端应用的反向代理. 业务需求:现在需要在java web应用端上传文件,同时还要能 ...

  2. VS2017 15.6之后支持直接反编译了

    在 15.6 预览版 2 中,增加了导航到反编译源功能. 启用后,在任何引用的类型或成员上调用转到定义或查看定义时,将显示其通过 ILSpy 反编译使用重新构造方法主体的定义. 要打开此功能,请转到“ ...

  3. 01.Redis 初体验

    0. Redis安装 官网下载Redis 解压缩 make make install 安装后的执行命令被保存在了/usr/local/bin目录下 1. 配置文件:redis.conf daemoni ...

  4. Object 中的wait和Thread中sleep的区别

    摘自 http://www.cnblogs.com/loren-Yang/p/7538482.html 一.区别 1.wait()来自于Object类而sleep来自于Thread类 2.sleep没 ...

  5. PHP:产生不反复随机数的方法

    来源:http://www.ido321.com/1217.html 不管是Web应用,还是WAP或者移动应用,随机数都有其用武之地.在近期接触的几个小项目中.我也经常须要和随机数或者随机数组打交道, ...

  6. Spring Data

    官网地址 http://projects.spring.io/spring-data 包含子项目 简介 Spring Data 是 Spring 的一个子项目.用于简化数据库访问,支持NoSQL,关系 ...

  7. mysql 半同步复制~ 整体概述与改进

    一 简介:今天来聊聊增强半同步复制这一强悍的特性 二 原理解析 1 AFTER_COMMIT(5.6默认值) master将每个事务写入binlog ,传递到slave 刷新到磁盘(relay log ...

  8. jquery禁用a标签

    jquery禁用a标签方法1 01 02 03 04 05 06 07 08 09 10 11 12 $(document).ready(function () {         $("a ...

  9. Django学习手册 - admin后台 切换成中文显示/添加数据表

    Django admin后台管理 切换成中文界面: 站点显示为中文: 在setting 里面修改 LANGUAGE_CORE = 'zh-Hans' 字段名显示中文 class Test(models ...

  10. 由JDBC而来的对Class.forName()用法发散

    昨日在帮一个学习java的小伙子指导JDBC链接数据库时,在对数据库驱动进行加载时,用到Class.forName(),一直都是照葫芦画瓢,对这种写法一直不是太理解,故查询了相关文档后,将心得记录一下 ...