题目链接: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. 使用Nginx反向代理绕过域名备案详解

    之前笔者在景安云搞过一个Wordpress博客,然后域名备案也是在景安云上面搞的,后来又搞了一个阿里云的服务器,想把博客迁移到阿里云并且使用Ghost博客,然后使用二级域名链接到阿里云,结果出事了.景 ...

  2. C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法

    C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 1.新建组件这里可以自定义一个Panel控件起名为PanelEx 2.增加一个BoderColor属性和BoderSize属性 pr ...

  3. linux的基本操作(LNMP的基本操作)

    LNMP 的环境搭建 和LAMP不同的是LNMP中的N指的是是Nginx(类似于Apache的一种web服务软件)其他都一样.目前这种环境应用的也是非常之多.Nginx设计的初衷是提供一种快速高效多并 ...

  4. maven deploy 上传jar包到私有仓库

    mvn \ deploy:deploy-file \ -DgroupId=com.weibo.datasys \ -DartifactId=data-flow \ -Dversion=2.0.0 \ ...

  5. php数组合并方法array_merge + 排序array_multisort方法 array_unique数组去重 array_values数组索引值重新从0开始递增

    $dingdan = array_merge($jie_dingdan,$user_dingdan);//数组合并方法 $orderFile = array(); foreach($dingdan a ...

  6. JQuery实现表格自动增加行,对新行添加事件

    实现功能: 通常在编辑表格时表格的行数是不确定的,如果一次增加太多行可能导致页面内容太多,反应变慢:通过此程序实现表格动态增加行,一直保持最下面有多个空白行. 效果: 一:原始页面 二:表1增加新行并 ...

  7. 怎么将APE转MP3,APE转MP3的方法

    怎样实现APE转MP3的问题呢?很多时候我们从网上所下载的音乐格式,可能并不是我们所需要的音乐格式.如APE音乐格式,那么当我们下载了自己并不需要的APE音乐格式我们应该如何将其转换为自己需要的MP3 ...

  8. java_工厂模式

    定义: 初学者总是把所有的代码写在一个类里面,这样是非常危险的,因为所有错误集中在一个类里了,而且代码一长,调试就很困难 所以参照工厂流水线,分车间分模块来写代码,在实际操作中也就是说将代码模块化,封 ...

  9. [No0000194]聊聊 Chrome DevTools 中你可能不知道的调试技巧

    对于前端开发者来说,ChromeDevTools 绝对是不可或缺的调试工具,我们常用的调试方法包含一些console等,而ChromeDevTools 其实很强大,下面来聊聊一些你可能不知道的debu ...

  10. LeetCode 237 Delete Node in a Linked List 解题报告

    题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...