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被选作起点,选与之相连的权值 ...
随机推荐
- 基于Redisson实现分布式锁
前言 最近开发了几个微服务上线了,发现定时任务执行了很多次,查看rancher发现这几个微服务都是多实例的,也就是说定时任务执行了多次,恰好所用框架中使用的是Redisson, 正好记录下使用Redi ...
- AVL树的JAVA实现及AVL树的旋转算法
1,AVL树又称平衡二叉树,它首先是一颗二叉查找树,但在二叉查找树中,某个结点的左右子树高度之差的绝对值可能会超过1,称之为不平衡.而在平衡二叉树中,任何结点的左右子树高度之差的绝对值会小于等于 1. ...
- 小程序Promise不支持finally解决方案
小程序Promise不支持finally解决方案 代码片段 点击链接即可在微信开发者工具中查看代码wechatide://minicode/t2eidemj7P3X git地址 基本思路 小程序的Pr ...
- HTML5 元素拖拽实现 及 jquery.event.drag插件
如上图片: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- Winform中使用WPF控件并动态读取Xaml
1.添加新项 2.在构造函数中加入 public partial class UserControl1 : UserControl { public UserControl1() { Initiali ...
- js针对数组的操作
链接:http://www.w3school.com.cn/jsref/jsref_obj_array.asp Array 对象方法 方法 描述 concat() 连接两个或更多的数组,并返回结果. ...
- Java EE之Struts2-2.5配置
开学以来,已经三周了.Java EE却不太走心,于是,这几日空杯心态,重新学习.复习了Java SE和Java Web开发技术,然后入手Struts2.为了使用最新版本的Structs2,我去官网下载 ...
- [C++]指针与引用(应用辨析)
1.指针变量允许将一个整数经强制转换后赋值给指针变量 Eg: float *fp; fp = (float *)5000;//意义:将5000作为一个地址赋给指针变量fp 2 ...
- configure编译选项
1.rpath与rpath-link的区别 参考链接:http://blog.csdn.net/xph23/article/details/38157491 rpath 是 运行时候链接的库, rpa ...
- JS判断页面是否出现滚动条
今天无聊,帮一个网友解决一个很无聊的问题,用JS判断页面是否出现滚动条,在网上看了一些代码,经过验证并不起作用,下面是在网上搜索到的代码: 当可视区域小于页面的实际高度时,判定为出现滚动条,即: if ...