Problem 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.
译文:农民约翰已经当选为他镇上的市长!他的竞选承诺之一是将互联网连接到该地区的所有农场。当然,他需要你的帮助。农夫约翰为他的农场订购了高速连接,并将与其他农民分享他的连接。为了降低成本,他希望铺设最少量的光纤将他的农场与其他农场连接起来。给出连接每对农场需要多少光纤的清单,您必须找到将它们连接在一起所需的最少光纤数量。每个服务器场必须连接到其他服务器场,以便数据包可以从任何一个服务器场流向其他服务器场。任何两个农场之间的距离不会超过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. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
译文:输入包括几种情况。对于每种情况,第一行包含农场的数量,N(3 <= N <= 100)。以下几行包含N×N视锥矩阵,其中每个元素显示从农场到另一个的距离。从逻辑上讲,它们是由N个空格分隔的整数组成的N行。当然,对角线将为0,因为从农场到农场的距离对这个问题并不有趣。

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算法,水过!
AC代码之Prim算法:
 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int n,mincost[maxn],cost[maxn][maxn];
bool vis[maxn];
int Prim(){
for(int i=;i<=n;++i)
mincost[i]=cost[][i];
mincost[]=;vis[]=true;
int res=;
for(int i=;i<n;++i){
int k=-;
for(int j=;j<=n;++j)
if(!vis[j] && (k==-||mincost[k]>mincost[j]))k=j;
if(k==-)break;
vis[k]=true;
res+=mincost[k];
for(int j=;j<=n;++j)
if(!vis[j])mincost[j]=min(mincost[j],cost[k][j]);
}
return res;
}
int main()
{
while(cin>>n){
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
cin>>cost[i][j];
memset(vis,false,sizeof(vis));
cout<<Prim()<<endl;
}
return ;
}

ACM_最短网络(最小生成树)的更多相关文章

  1. 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)

    洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...

  2. P1546 最短网络 Agri-Net题解(克鲁斯卡尔)

    P1546 最短网络 Agri-Net 那么这个题是一道最小生成树的板子题 在此讲解kruskal克鲁斯卡尔方法: 原理: 并查集在这里被用到: 众所周知:树满足这样一个定理:如果 图 中有n个节点并 ...

  3. 洛谷 P1546 最短网络 Agri-Net

    题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...

  4. 洛谷P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...

  5. 洛谷——P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...

  6. [图论]最短网络:prim

    最短网络 目录 最短网络 Description Input Output Sample Input Sample Output 解析 代码 Description 农民约翰被选为他们镇的镇长!他其中 ...

  7. [图论]最短网络:kruskal

    最短网络 目录 最短网络 Description Input Output Sample Input Sample Output 解析 代码 Description 农民约翰被选为他们镇的镇长!他其中 ...

  8. P1546 最短网络(codevs | 2627村村通)

    P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...

  9. COGS【831】最短网络

    831. [USACO 3.1] 最短网络 ★   输入文件:agrinet.in   输出文件:agrinet.out   简单对比 时间限制:1 s   内存限制:128 MB usaco/agr ...

随机推荐

  1. Django-REST-Framework JWT 实现SSO认证(上)

    一.什么是Django-REST-Framework? Django-REST-framework 是基于Django框架的一个web RESTful风格开发的框架,它可以实现API接口的快速开发,但 ...

  2. Python-函数和代码复用

    函数的定义与使用 >函数的理解与定义 函数是一段代码的表示 -函数是一段具有特定功能的.可重用的语句组 -函数是一种功能的抽象,一般函数表达特定功能 -两个作用:降低编程难度 和 代码复用 de ...

  3. 这可能是vue-cli最全的解析了……

    题言: 相信很多vue新手,都像我一样,只是知道可以用vue-cli直接生成一个vue项目的架构,并不明白,他究竟是怎么运行的,现在我们一起来研究一下... 一.安装vue-cli,相信你既然会用到v ...

  4. Linux下汇编语言学习笔记73 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  5. Win32编程API 基础篇 -- 2.一个简单的窗口 根据英文教程翻译

    一个简单的窗口 例子:简单的窗口 有时人们在IRC提问,”我应该怎样制作一个窗口”...嗯,这恐怕不是完全这么简单好回答!其实这并不难一旦你明白你在做什么,但在你得到一个可展示的窗口之前还有一些事情需 ...

  6. Bootstrap基础教程:tutorialspoint-bootstrap

    来自turorialspoint的Boostrap基础教程(英文),官网:https://www.tutorialspoint.com/bootstrap/index.htm 中文版:https:// ...

  7. 奇偶数对调,保持顺序 —— 剑指Offer

    这道题目 https://www.nowcoder.net/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&t ...

  8. 大话USB驱动之总线驱动程序

    转载注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/25040009 总线驱动是不用改的.内核都帮我们做好了.为了了解整个USB驱动的体 ...

  9. C#文件运行类的VB.NET版本号

    主要差别在于事件处理要採用AddHandler和RemoveHandler,以及AddressOf三个keyword,其他基本一样. VB的操作稍微繁琐.但仍然能够实现.

  10. Java创建和解析Json数据方法——org.json包的使用(转)

    org.json包的使用 1.简介   工具包org.json.jar,是一个轻量级的,JAVA下的json构造和解析工具包,它还包含JSON与XML, HTTP headers, Cookies, ...