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 ...
随机推荐
- Spring事物的属性
链接:https://www.nowcoder.com/questionTerminal/1c65d30e47fb4f59a5e5af728218cac4?orderByHotValue=2& ...
- vue 组件数据传递
vue组件化开发 主要为了把一个大功能拆分成若干个小的功能,解决高耦合问题,同时也方便开发人员维护. 从功能上组件可以分为木偶组件和功能组件. 木偶组件(为了接收数据,渲染数据,基本上是没有逻辑的 ...
- 045、安装Docker Machine (2019-03-08 周五)
参考https://www.cnblogs.com/CloudMan6/p/7223599.html 前面我们的实验中只有一个docker host ,所有的容器都是运行在这一个host上的.但在 ...
- SQL的六种约束
https://blog.csdn.net/z120270662/article/details/79501621
- SQL Server进阶(十)事务和并发处理
1 https://www.cnblogs.com/edisonchou/p/6129717.html
- Lua Doc生成工具
Luadoc http://keplerproject.github.io/luadoc/ Overview LuaDoc is a documentation generator tool for ...
- React 体验
https://github.com/facebook/create-react-app npm i -g create-react-app cd <your-folder> creat ...
- Andrew NG 机器学习编程作业3 Octave
问题描述:使用逻辑回归(logistic regression)和神经网络(neural networks)识别手写的阿拉伯数字(0-9) 一.逻辑回归实现: 数据加载到octave中,如下图所示: ...
- 转载-reduceByKey和groupByKey的区别
原文链接-https://www.cnblogs.com/0xcafedaddy/p/7625358.html 先来看一下在PairRDDFunctions.scala文件中reduceByKey和g ...
- confluence 5.8.6升级到5.10.1
1.下载最新版 https://www.atlassian.com/software/confluence/download 2.上传至服务器 tar zxf atlassian-confluence ...