POJ 1258 Agri-Net(Prim)
题目网址:http://poj.org/problem?id=1258
题目:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 60004 | Accepted: 24855 |
Description
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28 最小生成树水题~因为点数比较少,而且题目给的是邻接矩阵 所以用了Prim算法。 代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf=;
int n,res;
int dist[],f[][];
int vis[];
void prim(){
int v=;
memset(vis, , sizeof(vis));
for (int i=; i<=n; i++) dist[i]=inf;
dist[]=;
for (int i=; i<=n; i++) {
int Min=inf;
for (int j=; j<=n; j++) {
if(dist[j]<Min && !vis[j]){
Min=dist[j];
v=j;
}
}
vis[v]=;
res+=dist[v];
for (int j=; j<=n; j++) {
dist[j]=min(dist[j],f[v][j]);//算法核心,是求遍历点到部分最小生成树的最小距离,而不是单个点
}
}
}
int main(){
while (scanf("%d",&n)!=EOF && n) {
res=;
for (int i=; i<=n; i++) {
for (int j=; j<=n; j++) {
scanf("%d",&f[i][j]);
}
}
prim();
printf("%d\n",res);
}
return ;
}
POJ 1258 Agri-Net(Prim)的更多相关文章
- 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算法)
题意: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最小生成树模板题
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 ...
- 最小生成树 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 // 顶点的最大个数 ( ...
- 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> ...
- Prim算法求权数和,POJ(1258)
题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...
随机推荐
- redis的几个知识点
Redis的全称是Remote Dictionary Server,即远程字典服务,通常用作服务器缓存服务. 这里通过Redis的几个知识点来了解Redis. Redis的通讯协议 Redis的通讯协 ...
- Java 自定义注解 校验指定字段对应数据库内容重复
一.前言 在项目中,某些情景下我们需要验证编码是否重复,账号是否重复,身份证号是否重复等... 而像验证这类代码如下: 那么有没有办法可以解决这类似的重复代码量呢? 我们可以通过自定义注解校验的方式去 ...
- spring集成mybatis-plus
一.mybatis-plus 使用mybatis-plus可以轻松实现通用crue.通用service,不用再在xml.dao.service里写增删改查的代码(需要写特殊方法的时候可以按原先的来), ...
- [Linux] Vmware 15安装CentOs后显示网络不可用
转载自:https://blog.csdn.net/chenjin_chenjin/article/details/83617186 在虚拟机上安装好centos7后,ping www.baidu.c ...
- 按插入顺序排序的map
LinkedHashMap HashMap是无序的,HashMap在put的时候是根据key的hashcode进行hash然后放入对应的地方.所以在按照一定顺序put进HashMap中,然后遍历出Ha ...
- 【linux】【NodeJs】Centos7安装node-v10.16.3环境
前言 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. https://node ...
- (intellij ieda激活码、CLion激活码、php storm激活码、webstorm激活码、jetbrains全家桶激活码)
中华民族传统美德 下载地址 https://www.jetbrains.com/zh/phpstorm/promo/?utm_source=baidu&utm_medium=cpc&u ...
- java、if判断和循环
一.选择.循环语法 选择 if if(表达式)语句A: 如果表达式的值是真的,就会执行语句A,否则不执行 ...
- Linux 伪终端(pty)
通过<Linux 终端(TTY)>一文我们了解到:我们常说的终端分为终端 tty1-6 和伪终端.使用 tty1-6 的情况一般为 Linux 系统直接连了键盘和显示器,或者是使用了 vS ...
- Gradle 梳理:安装、入门使用方法
Gradle 教程:第一部分,安装[翻译] 原文地址:http://rominirani.com/2014/07/28/gradle-tutorial-part-1-installation-se ...