最小生成树

对于必须要加入的边, 让其边权为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. tomcat自动重新加载应用

    前言 当应用配置文件发生变化时,无需重启tomcat,可以使tomcat重新加载应用. 场景 假设存在一个J2EE应用A,对应war文件名称为A.war,部署在tomcat的webapps目录下,即: ...

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

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

  3. SpringBoot系列: 与Spring Rest服务交互数据

    不管是单体应用还是微服务应用, 现在都流行Restful风格,  下图是一个比较典型的使用rest的应用架构, 该应用不仅使用database数据源, 而且用到了一个Weather微服务, 另一方面, ...

  4. js中文编码到C#后台解码

    escape() 方法: 采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编 ...

  5. 苹果手机上点击WEUI日期控件不容易点中

    主要问题是WEUI cells有Padding,而苹果手机一般屏幕较小,容易点不中导致的 <div class='weui_cell' style="padding-bottom:0p ...

  6. PowerDesigner设置一对一关系

    (1)修改Cardinalities 为One-one (2)设置Dominant role A->B(假设表A,表B),保存 (3)到Joins页,设置Parent为None,设置Parent ...

  7. luogu P4770 [NOI2018]你的名字

    传送门 upd 19.4.24: WC这个做法真的有问题,不往回跳会WA是因为一开始跳到了S[1...l-1]所对应的点,然后往后接字符的时候可能会因为不在正确的endpos中,然后往回跳过头,其实一 ...

  8. mysql 架构 ~ MGR 与PXC的对比

    一 简介 MGR和PXC的对比 二  WriteSet1 定义 是组件对于写节点应用事务生成binlog的再封装,用来验证其他节点的事务冲突 PXC构成key db_table_组件值data bin ...

  9. (2)Java数据结构--二叉树 -和排序算法实现

    === 注释:此人博客对很多个数据结构类都有讲解-并加以实例 Java API —— ArrayList类 & Vector类 & LinkList类Java API —— BigDe ...

  10. TensorFlow学习笔记之--[tf.clip_by_global_norm,tf.clip_by_value,tf.clip_by_norm等的区别]

    以下这些函数可以用于解决梯度消失或梯度爆炸问题上. 1. tf.clip_by_value tf.clip_by_value( t, clip_value_min, clip_value_max, n ...