Agri-Net - poj 1258 (Prim 算法)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44373 | Accepted: 18127 |
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
#include <stdio.h>
#define INF 100000
int n;
int map[][];
int dis[],vis[];
int Prim(){
for(int i=;i<n;i++){
vis[i]=;
dis[i]=INF;
}
dis[]=; for(int i=;i<n;i++){
int min=INF;
int p;
for(int j=;j<n;j++){
if(!vis[j]&&min>dis[j]){
min=dis[j];
p=j;
}
}
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]>map[p][j]){
dis[j]=map[p][j];
}
}
}
for(int i=;i<n;i++){
dis[]+=dis[i];
}
return dis[];
}
int main() {
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&map[i][j]);
}
}
int min=Prim();
printf("%d\n",min);
}
return ;
}
Agri-Net - poj 1258 (Prim 算法)的更多相关文章
- Highways - poj 2485 (Prim 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24383 Accepted: 11243 Description T ...
- Truck History - poj 1789 (Prim 算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20884 Accepted: 8075 Description Ad ...
- Agri-Net POJ - 1258 prim
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ; #defi ...
- POJ 1258 Agri-Net(Prim算法)
题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...
- POJ 1258 Agri-Net(Prim)
题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net (Prim&Kruskal)
题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...
- POJ 1258:Agri-Net(最小生成树&&prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38918 Accepted: 15751 Descri ...
- poj 1258 Agri-Net(Prim)(基础)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44487 Accepted: 18173 Descri ...
随机推荐
- unity3d 场景配置文件生成代码
using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...
- Matrix.LookAtLH()和Matrix.LookAtRH()所表达的涵义
function lookAtLH(eye:Vector3D, at:Vector3D, up:Vector3D) 一个摄像机矩阵可有由三个部分组成:摄像机位置.目标位置以及摄像机上下方.对应的就是上 ...
- PHP开发环境配置系列(四)-XAMPP常用信息
PHP开发环境配置系列(四)-XAMPP常用信息 博客分类: PHP开发环境配置系列 xamppphp 完成了前面三篇后(<PHP开发环境配置系列(一)-Apache无法启动(SSL冲突)> ...
- Python学习笔记——基本数据结构
列表list List是python的一个内置动态数组对象,它的基本使用方式如下: shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I ...
- 用android连小米手机做profiling
安装失败 Installation failed with message Failed to finalize session http://www.jianshu.com/p/fea7d96385 ...
- ELKStack日志离线系统
通过Filebeat抽取数据到logstash中,转存到ElasticSearch中,最后通过Kibana进行展示 https://www.ibm.com/developerworks/cn/open ...
- 转: NetBean 远程开发的好文2 --> 工欲善其事,必先利其器系列--Netbeans之远程开发
转自: http://www.cnblogs.com/zuoca/archive/2012/07/09/Remote_Development_With_Netbeans_origin.html 实践 ...
- Binder与interface
在Interface中,asBinder函数涌来将服务类接口类型转换为IBinder类型: 相反的,asInterface函数用来将Ibinder类型转换为服务接口类型
- 设计模式——浅复制VS深复制
背景 在学习原型模式的时候,採用了一个差别与其它模式的新方法.採用了"克隆(Clone)方法.通过实现ICloneable接口中的Clone()方法来达到克隆的目的. 代码实现过程中,存在了 ...
- linux生成指定大小的文件(转)
# dd if=/dev/zero of=50M.file bs=1M count=50在当前目录下生成一个50M的文件 虚拟块设备文件更通用的名称是硬盘镜像文件(Hard Disk Image),但 ...