题目链接:http://poj.org/problem?id=1258

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
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

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

 
学学prim
 //poj1258 prim算法
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
int cost[maxn][maxn];//表示边的权值不存在的情况下为INF
int mincost[maxn];//从集合x出发的边到每个顶点的最小权值
bool used[maxn];//顶点i是否包含在集合x中
int n;//顶点数 int prim()
{
for(int i=;i<n;i++){
mincost[i]=INF;
used[i]=false;
}
mincost[]=;
int res=;
while(true){
int v=-;//从不属于x的顶点中选取从x到其权值最小的顶点
for(int i=;i<n;i++){
if(!used[i]&&(v==-||mincost[i]<mincost[v])){
v=i;
}
}
if(v==-) break;
used[v]=true;//把顶点v加入x
res+=mincost[v];//把边的长度加到结果里
for(int i=;i<n;i++){
mincost[i]=min(mincost[i],cost[v][i]);
}
}
return res;
}
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
cin>>cost[i][j];
}
}
cout<<prim()<<endl;
}
return ;
}

脑子笨,就得不停地重复重复再重复

 #include <iostream>
#include <algorithm>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
int n;
int cost[maxn][maxn];
int used[maxn];
int mincost[maxn];
int res;
void prim()
{
for(int i=;i<n;i++){
mincost[i]=INF;
used[i]=;
}
mincost[]=;
res=;
while(){
int v=-;
for(int i=;i<n;i++) if(!used[i]&&(v==-||mincost[i]<mincost[v])) v=i;
if(v==-) break;
res+=mincost[v];
used[v]=;
for(int i=;i<n;i++) mincost[i]=min(mincost[i],cost[v][i]);
}
}
int main()
{
while(cin>>n){
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>cost[i][j];
prim();
cout<<res<<endl;
}
return ;
}

once again ...

 #include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=;
int n;
int a[N][N];//各点之间的距离
int mina[N];
int used[N];
int res;
int prim()
{
for(int i=;i<n;i++){
mina[i]=INF;
used[i]=;
}
mina[]=;
res=;
while(){
int v=-;
for(int i=;i<n;i++){
if(!used[i]&&(v==-||mina[i]<mina[v])) v=i;
}
if(v==-) break;
used[v]=;
res+=mina[v];
for(int i=;i<n;i++){
mina[i]=min(mina[i],a[v][i]);
}
}
return res;
}
int main()
{
while(cin>>n){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
cin>>a[i][j];
}
}
cout<<prim()<<endl;
}
return ;
}

Poj1258 Agri-Net (最小生成树 Prim算法 模板题)的更多相关文章

  1. 图的最小生成树prim算法模板

    用prim算法构建最小生成树适合顶点数据较少而边较多的图(稠密图) prim算法生成连通图的最小生成树模板伪代码: G为图,一般为全局变量,数组d为顶点与集合s的最短距离 Prim(G, d[]){ ...

  2. 最小生成树-prim算法模板

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<= ...

  3. 最小生成树prim算法———模板

    codevs.cn 最优布线问题 #include<cstdio>#include<cstring> bool u[101]; int g[101][101],minn[101 ...

  4. 最小生成树—prim算法

    最小生成树prim算法实现 所谓生成树,就是n个点之间连成n-1条边的图形.而最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int map[ ...

  5. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  6. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  7. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  8. Highways POJ-1751 最小生成树 Prim算法

    Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...

  9. SWUST OJ 1075 求最小生成树(Prim算法)

    求最小生成树(Prim算法) 我对提示代码做了简要分析,提示代码大致写了以下几个内容 给了几个基础的工具,邻接表记录图的一个的结构体,记录Prim算法中最近的边的结构体,记录目标边的结构体(始末点,值 ...

随机推荐

  1. JNDI数据源的配置

    一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Conn ...

  2. Mysql Window 解压版 忘记密码

    1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd1窗口,切换到mysql的bin目录,运行命令: mysq ...

  3. Kafka认证权限配置(动态添加用户)

    之前写过一篇Kafka ACL使用实战,里面演示了如何配置SASL PLAINTEXT + ACL来为Kafka集群提供认证/权限安全保障,但有一个问题经常被问到:这种方案下是否支持动态增加/移除认证 ...

  4. Visual Studio 2015编译wxWidgets

    宫指导说,换帅如换刀 程序员的编译器一换,基本套路必须都重练几次 使用wxWidgets并不难,但不能使用现有的库和工程配置文件,细节就必须理清楚 获取wxWidgets 官方的下载页面,下7z或zi ...

  5. 禅道docker化(Centos7.2)

    操作步骤 确认服务器禅道版本及容器禅道版本 服务器禅道版本:9.6.2 容器禅道版本:9.6.3 版本sql比对 下载官方9.6.3源码包url:http://dl.cnezsoft.com/zent ...

  6. java 的访问权限控制

    package test06; public class PermissionModel { private int age; public String name; public int getAg ...

  7. N - Asteroids

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  8. linux的基本操作(磁盘管理)

    磁盘管理 [查看磁盘或者目录的容量 df 和 du] df 查看已挂载磁盘的总容量.使用容量.剩余容量等,可以不加任何参数,默认是按k为单位显示的 df常用参数有 –i -h -k –m等 -i 使用 ...

  9. [转] - xargs 分析

    原文出处:http://www.cnblogs.com/f-ck-need-u/p/5925923.html 学习这个xargs花了很长时间,在网上翻了很久也查了很多书关于xargs的介绍,都只是简单 ...

  10. Oracle课程档案,第六天

    体系结构: instance:实例 database:数据库 RAC:多实例对一个数据库 SGA:最大总数 (系统全局区域)缓存区 PGA:其中的一块, 也是一个缓存区 server process: ...