POJ 1258 Agri-Net (Prim&Kruskal)
题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通。求边的最小总花费。
思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复杂度O(n^2)
prim:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std; int mat[110][110];
bool vis[110];
int d[110]; int Prim(int n) {
int ans = 0;
int p = 0;
vis[0] = 1;
for (int j = 1; j < n; ++j) {
d[j] = mat[p][j];
}
for (int i = 1; i < n; ++i) {
p = -1;
for (int j = 1; j < n; ++j) {
if (vis[j]) continue;
if (p == -1 || d[j] < d[p]) {
p = j;
}
}
ans += d[p];
vis[p] = 1;
for (int j = 1; j < n; ++j) {
if (vis[j]) continue;
d[j] = min(d[j], mat[p][j]);
}
}
return ans;
} int main() {
int n, i, j;
while (scanf("%d", &n) != EOF) {
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
scanf("%d", &mat[i][j]);
}
}
int ans = Prim(n);
printf("%d\n", ans);
}
return 0;
}
kruskal:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std; int N; // 节点数量
struct edge {
int from, to, dist;
bool operator<(const edge &b) const {
return dist < b.dist;
}
} es[10006];
int par[105];
void init() {
for (int i = 1; i <= N; ++i) par[i] = i;
}
int find(int x) {
return x == par[x] ? x : par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) par[x] = y;
}
int kruskal() {
int res = 0;
init();
int E = N*N;
sort(es + 1, es + 1 + E);
for (int i = 1; i <= E; ++i) {
edge e = es[i];
//printf("u:%d v:%d d:%d\n", e.from, e.to, e.dist);
if (find(e.from) != find(e.to)) {
unite(e.from, e.to);
res += e.dist;
}
}
return res;
}
void solve() {
cout << kruskal() << endl;
}
int main()
{
while (cin >> N) {
int d;
int id;
for (int u = 1; u <= N; ++u)
for (int v = 1; v <= N; ++v) {
cin >> d;
id = (u - 1)*N + v;
es[id].from = u;
es[id].to = v;
es[id].dist = d;
}
solve();
}
return 0;
}
POJ 1258 Agri-Net (Prim&Kruskal)的更多相关文章
- POJ 1258 Agri-Net(Prim算法)
题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...
- POJ 1258 Agri-Net (最小生成树+Prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39820 Accepted: 16192 Descri ...
- POJ 1258 Agri-Net(Prim)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41230 Accepted: 16810 Descri ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1258 Agri-Net (prim水题)
Agri-Net Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Subm ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
#include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...
- 最小生成树(prim&kruskal)
最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法: 原始的加权连通图——————D被选作起点,选与之相连的权值 ...
随机推荐
- 读取Excel中数据
#region 读取导入Excel数据 /// <summary> /// /// </summary> /// <param name="filename&q ...
- [C++]数组与指针(纯代码-复习用)
#include<iostream> #include<cmath> //C++ //#include<math.h> //C #include<ctime& ...
- spoj gss1 gss3
传送门 gss1 gss3 spoj gss系列=最大字段和套餐 gss1就是gss3的无单点修改版 有区间查询和单点修改,考虑用线段树维护 我们要维护区间权值和\(s\),区间最大前缀和\(xl\) ...
- kan
http://blog.csdn.net/yahohi/article/details/7427724 http://duanhengbin.iteye.com/blog/1706635 http:/ ...
- js_原生js实现上拉加载更多的功能。
1.人生啊,是我莽撞了啊.这是我公司一个喜欢读书的女孩子的个性签名,喜欢哪些句子,不悲伤,却切切实实的令人喜好. 2.写程序是一件很直接明了的事情,明白了就是明白了,不懂就是不懂,不懂装懂的会让你走很 ...
- 第一次安卓android studio安装,常见问题。
出处:纸月 托了很久终于开始学习安卓了,之前看课本教程<第一行代码>用的是eclipse,但后来它不支持了就决定用android studio,第一次安装就出现了一些小的问题 第一个是关于 ...
- curl命令下载jdk
第一步:找到下载地址 第二步:下载
- scp -r拷贝目录不会拷贝软连接
scp -r拷贝目录,不会拷贝 软连接的 解决方法: 使用rsync拷贝 参考:rsync本地及远程复制备份[原创] - paul_hch - 博客园 https://www.cnblogs.com/ ...
- php数据库的增删改查
1.查询: 数据的显示,这里就可以嵌入php来进行数据的输出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- Bootstrap3.0学习第二轮(栅格系统原理)
详情请查看 http://aehyok.com/Blog/Detail/8.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...