HDU 1102
最小生成树
对于必须要加入的边, 让其边权为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的更多相关文章
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- 图论问题(2) : hdu 1102
题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是 ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- while应用和函数学习
# ******************************练习****************************# 在控制台中获取两个整数,作为循环开始和结束的点'''a = int(in ...
- JavaScript 实现一个简单的MVVM前端框架(ES6语法)
前言 随着前端各大框架的崛起,为我们平时的开发带来了相当的便利,我们不能一直停留在应用层面,今天就自己动手实现一个乞丐版的MVVM小框架 完整代码github地址 效果 html代码 <div ...
- 微信接口 output {"errMsg":"translateVoice:fail, the permission value is offline verifying"}
jsApiList : [ 'checkJsApi', 'startRecord', 'stopRecord','translateVoice','scanQRCode', 'openCard' ]增 ...
- 使用jQuery插件时避免重复引入jquery.js文件
当一个页面使用多个jQuery插件时,需要避免重复引入jquery.js文件,因为后面映入的jQuery.js文件中定义的jQuery对象会覆盖掉前面的jQuery对象,导致之前定义的jQuery插件 ...
- 27. Spring Boot 缓存注解详解: @Cacheable、@CachePut、 @CacheEvict、@Caching、@CacheConfig
1.使用OGNL的命名规则来定义Key的值 @Cacheable(cacheNames = {"user"},key = "#root.methodName + '[' ...
- ****** 二十八 ******、软设笔记【数据库】-分布式数据库、特点、数据存储、DBMS组成
分布式数据库 一.分布式数据库 分布式数据库由一组数据组成,这些数据物理上分布在计算机网络的不同结点(场地)上,逻辑上是属于同一个系统.每个结点可以执行局部应用,也能通过网络通信子 ...
- Nginx正反向代理、负载均衡等功能实现配置
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 系统环境: VirtualBox Manager Centos6.4 nginx1.10.0 IP对应的机器名: IP ...
- tensorflow 学习1——tensorflow 做线性回归
. 首先 Numpy: Numpy是Python的科学计算库,提供矩阵运算. 想想list已经提供了矩阵的形式,为啥要用Numpy,因为numpy提供了更多的函数. 使用numpy,首先要导入nump ...
- keras中的重要函数
https://blog.csdn.net/u012969412/article/details/70882296
- CTF 湖湘杯 2018 WriteUp (部分)
湖湘杯 2018 WriteUp (部分),欢迎转载,转载请注明出处! 1. CodeCheck(WEB) 测试admin ‘ or ‘1’=’1’# ,php报错.点击登录框下面的滚动通知,URL ...