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 ...
随机推荐
- Chrome刷新缓存
Ctrl+Shift+Del 清除Google浏览器缓存的快捷键 Ctrl+Shift+R 重新加载当前网页而不使用缓存内容
- 【转】javascript代码混淆和压缩
隐藏 JavaScript 源代码?不,你只能混淆和压缩JavaScript源代码 http://www.yaohaixiao.com/tools/confuse-and-compressing-ja ...
- Slider绑定事件,初始化NullPointerException错误
最近刚刚接触Silverlight,随便在网上找了一个入门的博文http://www.cnblogs.com/Terrylee/archive/2008/03/07/Silverlight2-step ...
- vue 中 使用百度编辑器 UEditor
(单页应用,多编辑器也可行) 新建一个Ueditor.vue组件对象,该组件用来封装ueditor,用来进行复用. <template> <div> <!--下面通过传递 ...
- 复习java web之jsp入门_El表达式_JSTL标签库
JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...
- c# c/s 框架的分页用户控件,还有事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- Vue.js 技术揭秘(学习) 深入响应式原理 nextTick外传
microTask mutationObserve. promise.then macroTask setImmediate. messageChannnel.setTimeout.postMess ...
- Leetcode#832. Flipping an Image(翻转图像)
题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...
- Web三层-UI/BLL/DAL/MODEL
2013传智播客视频\视频\2013-05-28-EF\视频 创建4个程序集,添加引用,model添加映射, P01UI表现层--BLL+MODELP02BLL业务层--DAL+MODELP03DAL ...
- java语言什么时候诞生的?
java语言什么时候诞生的?创始人是谁?何时发布的? Java编程语言是sun Microsystems公司JamesGosling在1990年创建的1995年公布于世