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被选作起点,选与之相连的权值 ...
随机推荐
- 51NOD-1486 大大走格子
有一个h行w列的棋盘,里面有一些格子是不能走的,现在要求从左上角走到右下角的方案数. Input 单组测试数据. 第一行有三个整数h, w, n(1 ≤ h, w ≤ 10^5, 1 ≤ n ≤ 20 ...
- 散列之HashTable学习
1,什么是散列? 举个例子,在日常生活中,你将日常用品都放在固定的位置,当你下次需要该东西时,直接去该地方取它.这个过程就相当于散列查找. 若将它们随意杂乱无章地存放,当需要某件东西时,只能一个地方一 ...
- C# 摇奖机实例(线程)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- node版本管理工具nvm安装以及使用
curl命令安装 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash 或者 使用wg ...
- cetus系列~ cetus+mha
一 简介:mha+cetus高可用架构二 环境 1 mysql 5.7 并行复制+GTID 2 cetus最新版 3 mha0.57二 安装 1 安装mha-rpm包 2 做免密认证 3 ...
- ROS学习笔记(二) # ROS NodeHandles
1. 自动启动和关闭 ros::NodeHandle nh: 这段代码执行之后,如果内部节点还没有启动,ros::NodeHandle 会启动这个节点:一旦所有的 ros::NodeHandle 实例 ...
- Theano教程:Python的内存管理
在写大型程序时候的一大挑战是如何保证最少的内存使用率.但是在Python中的内存管理是比较简单的.Python显示分配内存,使用引用计数系统管理对象,当指向某一个对象的引用数变为 0 的时候,该对象所 ...
- [转]AMBA、AHB、APB、ASB总线简介
[转]http://www.cnblogs.com/zhaozhong1989/articles/3092140.html 1.前言 随着深亚微米工艺技术日益成熟,集成电路芯片的规模越来越大.数字IC ...
- python中对列表和循环使用的小练习
#author devilf product_list = [ (), (), (), (), () ] shop_list = [] salary = input('pls enter your s ...
- mtk 无线配置文件生效过程
openwrt 下无线接口的配置文件位于 /etc/config/wirless 中. 启动 /sbin/wifi 脚本后,生效过程如下: (1)通过 uci2dat 工具生成所需要的 .dat文件 ...