题目链接: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. IE 浏览器旧版本下载

    1. http://www.oldversion.com/windows/internet-explorer/ IE10 浏览器 32bit & 64 bit:下载 2. https://ww ...

  2. TypeSrcript如何引入第三方库 如果加d.ts以及async await如何使用 demo,只有代码,文字后续补充

    https://files.cnblogs.com/files/cappuccino/laya2.rar

  3. J.U.C

  4. springboot中Properties注解的实现

    关于@PropertySources注解的理解:http://www.imooc.com/article/252889?block_id=tuijian_wz public interface Pro ...

  5. R 540

    好久没写题解了嘻嘻嘻,昨天补edu自闭了一天还没补完fg这div3令人愉悦. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a ...

  6. ArcGIS AddIN 与ArcMap自带工具进行交互

    参考示例代码:C:\Program Files (x86)\ArcGIS\DeveloperKit10.1\Samples\ArcObjectsNet\Brushing 核心代码: //获取Selec ...

  7. JS setAttribute兼容

    问题和表现: 最近实践中遇到的问题,setAttribute()设置在IE7中,无法设置style等属性.这样就对设置样式带了很大的困扰,例如绑定点击事件来隐藏元素,setAttribute(”sty ...

  8. Spring-Boot构建多模块项目

    Spring-Boot构建多模块项目 功能模块单独项目开发,可以将一个庞大的项目分解成多个小项目,便于细分开发 Maven多模块项目不能独立存在,必须有一个介质来包含. 1.创建一个Maven 项目, ...

  9. 利用python+selenium在pycharm下进行页面登陆的半自动测试

    很久没有写了,现在正式入职,准备好好干,加油! 我的第一个较正式的测试代码: from selenium import webdriverimport unittestimport sysimport ...

  10. POI操作Excel详解,读取xls和xlsx格式的文件

    package org.ian.webutil;   import java.io.File; import java.io.FileInputStream; import java.io.FileN ...