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 ...
随机推荐
- 记录工作中linux相关操作
在项目部署之后,查看日志能查看部署结果是否正确部署. 最开始查看日志我会使用cat service.log tail -f service.log vim service.log 打开日志之后 /+查 ...
- HTML文档简介
HTML简介 HTML标签 html文档标签: html源代码就好像word文档,有特殊的语法结构定义自己的功能. html文档标签 html标签,其下由两个主要节点标签head.body. head ...
- vim编辑python脚本时Tab补全
所属分类:成长之路 使用Linux写python脚本的时候,初期最痛苦的是什么?当然是各种库的不熟悉,知道了库,里面的方法还要挨个看,挨个记. 所以这时候,很多小伙伴使用了ipython,最强大的功能 ...
- [Leetcode] 第324题 摆动排序II
一.题目描述 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums ...
- 使用SpringDataRedis的入门
在使用ssm框架下,我们会到redis做缓存. 1> 第一步,导包. <!-- Redis客户端 --> <dependency> <groupId>redi ...
- JavaScript DOM 编程艺术
最近把JavaScript DOM 编程艺术这本书看完了,觉得这本书很好 深入浅出地展示了渐进增强.平稳退化.结构和样式分离等编程思想,我对书中重要的知识进行了梳理总结. 一.网页 二.JavaScr ...
- SpringBoot系列——ElasticSearch
前言 本文记录安装配置ES环境,在SpringBoot项目中使用SpringData-ElasticSearch对ES进行增删改查通用操作 ElasticSearch官网:https://www.el ...
- Spark Streaming 入门
概述 什么是 Spark Streaming? Spark Streaming is an extension of the core Spark API that enables scalable, ...
- 使用servlet+jdbc+MD5实现用户加密登录
/** * 分析流程: * 1.前端页面提交登录请求 * 2.被web.xml拦截,进入到LoginServlet(有两种方式:方式一,在web.xml文件中配置servlet拦截器;方式二,不用在w ...
- Python基础(十八)
今日主要内容 包 一.包 (一)什么是包 只要是含有__init__.py文件的文件夹就是一个包 包的本质其实就是一个文件夹,利用包将不同功能的模块组织起来,以此来提高程序的结构性和可维护性 包是用来 ...