题目:

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

思路:最小生成树模板题用prim算法多组输入 注意数组清空

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=110;
int n,tmp;
int mp[maxn][maxn],dis[maxn],vis[maxn];int main(){while(~scanf("%d",&n)){memset(mp,0,sizeof(mp));memset(dis,0,sizeof(dis));memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++){dis[i]=inf;for(int j=1;j<=n;j++){scanf("%d",&mp[i][j]);}}for(int i=1;i<=n;i++){dis[i]=mp[1][i];}dis[1]=0;vis[1]=1;int sum=0;for(int i=1;i<=n;i++){tmp=inf;int minn=inf;for(int j=1;j<=n;j++){if(vis[j]==0 && dis[j]<minn){tmp=j;minn=dis[j];}}if(tmp==inf) break;vis[tmp]=1;sum+=minn;for(int j=1;j<=n;j++){if(vis[j]==0 && dis[j]>mp[tmp][j])dis[j]=mp[tmp][j];}}printf("%d\n",sum);}return 0;
}

转载于:https://www.cnblogs.com/whdsunny/p/10528507.html

POJ 1258 Agri-Net (最小生成树)相关推荐

  1. 最小生成树模板 POJ——1258

    最小生成树是一个比较简单数据结构,形成最小生成树的方式有两种. 最小生成树是有图生成树,保证树的每条边的权值之和最小的生成树就叫做最小生成树,这一类的题目起初比较基础,主要是熟悉模板,POJ 1258 ...

  2. 【OpenJ_Bailian - 1258】【POJ - 1258】Agri-Net (最小生成树裸题)

    题干: Farmer John has been elected mayor of his town! One of his campaign promises was to bring intern ...

  3. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  4. poj 1789 Truck History(最小生成树 prim)

    题目:http://poj.org/problem?id=1789 大意:每个卡车都有自己的编号,由七位字母组成 d(to,td) is the distance of the types指t0 和 ...

  5. poj 3026 Borg Maze (最小生成树+bfs)

    有几个错误,调试了几个小时,样例过后 1Y. 题目:http://poj.org/problem?id=3026 题意:就是让求A们和S的最小生成树 先用bfs找每两点的距离,再建树.没剪枝 63MS ...

  6. POJ 2485 Highways(最小生成树 Prim)

    Highways   大意:给你一个用邻接矩阵形式存储的有n个顶点的无向图,让你求它的最小生成树并求出在这个生成树里面最大的边的权值. 思路:用Prim求,判断条件改一下就行. PS:dis数组初始化 ...

  7. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  8. 2019.9.18最小生成树知识点总结

    HDU 4081 Qin Shi Huang's National Road System(次小生成树-Kruskal) 博主的方法很好,但是有疑问,为什么不能将最多人口的两城市的距离设置为0,在进行 ...

  9. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

最新文章

  1. (android控件)ListView的Item中设置按钮实现
  2. Anaconda闪退问题
  3. linux下mysql开启远程访问权限及防火墙开放3306端口(mysql开放host访问权限)
  4. Unity3D 装备系统学习Inventory Pro 2.1.2 基础篇
  5. 多功能网址导航源码 包含交易系统等多功能
  6. android activity详解一:概述
  7. Tomcat中的Out Of Memory错误
  8. Maya+3dsMax三维建模
  9. rtt面向对象oopc——5.IO设备管理之快速查看设备父类调用设备子类的方法
  10. Linux安装MYSQL5.7教程(一次成功)
  11. 快手小视频批量下载高清无水印软件 快手短视频批量下载高清无水印软件
  12. 报文解析_101规约报文格式定义解析
  13. mysql 获取百分比函数,并对结果保留2位小数。
  14. vs点击方法跳不到对于的地方_田宫四驱车 狂牛 配VS底盘改装制作全攻略
  15. 操作系统——文件存储管理
  16. Aspose.Slides使用教程:使用 C++ 访问或修改 PowerPoint 文件的属性
  17. spring接管mybatis
  18. 学一点Wi-Fi: CCMP
  19. PDF处理软件:无法加注释加高亮(解密PDF等)
  20. 密码学基础(二)单表---置换密码 凯撒密码 棋盘密码 乘法密码 仿射密码 多表---vigenere方阵

热门文章

  1. Fiddle用于移动端抓包
  2. git-svn — 让git和svn协同工作
  3. javascript: 数组
  4. MSChart中转义符
  5. 【转】SQL Server中行列转换 Pivot UnPivot
  6. 进制转换c语言代码_奇怪的C语言代码,有些函数在变量前加上(void)是什么类型转换?...
  7. matlab regionprops区域属性信息
  8. 电脑word在哪_Word论文里的公式怎么编辑?这4个小工具帮你一分钟搞定!
  9. matlab的max与min函数
  10. java eden space_JVM虚拟机20:内存区域详解(Eden Space、Survivor Space、Old Gen、Code Cache和Perm Gen)...