Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 51685   Accepted: 21558

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
【题意】有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,
并且要使光纤距离最小,输出安装光纤的总距离
【分析】又是一个最小生成树,因为给出了一个二维矩阵代表他们的距离,直接算prim就行了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
int n,m,k,edg[N][N],lowcost[N],pre[N];
void Prim() {
for(int i=;i<=n;i++){
lowcost[i]=edg[][i];
}
lowcost[]=-;
int sum=;
for(int i=;i<n;i++){
int minn=inf;
for(int j=;j<=n;j++){
if(lowcost[j]!=-&&lowcost[j]<minn){
minn=lowcost[j];
k=j;
}
}sum+=minn;
lowcost[k]=-;
for(int j=;j<=n;j++){
if(edg[j][k]<lowcost[j]){
lowcost[j]=edg[j][k];
}
}
}
printf("%d\n",sum);
}
int main() {
while(~scanf("%d",&n)) {
memset(edg,,sizeof(edg));
memset(lowcost,,sizeof(lowcost));
for(int i=; i<=n; i++) {
for(int j=;j<=n;j++){
scanf("%d",&edg[i][j]);
}
}
Prim();
}
return ;
}

POJ1258 Agri-Net(Prim)的更多相关文章

  1. POJ1258 (最小生成树prim)

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46319   Accepted: 19052 Descri ...

  2. c/c++ 用普利姆(prim)算法构造最小生成树

    c/c++ 用普利姆(prim)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: ​ 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路.这时 ...

  3. HDU.1233 还是畅通工程(Prim)

    HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...

  4. ZOJ - 1586 QS Network (Prim)

    ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...

  5. 普利姆算法(prim)

    普利姆算法(prim)求最小生成树(MST)过程详解 (原网址) 1 2 3 4 5 6 7 分步阅读 生活中最小生成树的应用十分广泛,比如:要连通n个城市需要n-1条边线路,那么怎么样建设才能使工程 ...

  6. 普里姆算法(Prim)

    概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图(带权图)里搜索最小生成树.即此算法搜索到的边(Edge)子集所构成的树中,不但包括了连通图里的所有顶点(Vertex)且其所有边的权 ...

  7. 普里姆(Prim)算法

    概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图(即"带权图")里搜索最小生成树.即此算法搜索到的边(Edge)子集所构成的树中,不但包括了连通图里的所有顶点(V ...

  8. poj1258 Agri-Net (prim+heap)

    题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...

  9. 最小生成树练习3(普里姆算法Prim)

    风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...

随机推荐

  1. 【题解】ZJOI2008骑士

    树型打牌:洛谷P2607 这道题目一开始没有想到解法,只是想到没有上司的舞会,觉得十分的类似呀. 之后发现:n个点,n条边,只要删去一条边,就变成了和上题一模一样的做法. 那么考虑删去的这条边,实际上 ...

  2. 理解NLP中的卷积神经网络(CNN)

    此篇文章是Denny Britz关于CNN在NLP中应用的理解,他本人也曾在Google Brain项目中参与多项关于NLP的项目. · 翻译不周到的地方请大家见谅. 阅读完本文大概需要7分钟左右的时 ...

  3. React & Redux 的一些基本知识点

    一.React.createClass 跟 React.Component 的区别在于后者使用了ES6的语法,用constructor构造器来构造默认的属性和状态. 1. React.createCl ...

  4. POJ2559 Largest Rectangle in a Histogram (单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012 ...

  5. Install the Active Directory Administration Tools on Windows Server

    安装 Active Directory 管理工具 To manage your directory from an EC2 Windows instance, you need to install ...

  6. (转)Notepad++

    包括notepad++的详细的安装过程,插件使用说明,技巧…… crifan http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/ ...

  7. 转:RBAC权限控制

    名词解释: RBAC:Role-Based Access Control,基于角色的访问控制   关键词: RBAC,Java Shiro,Spring Security,   一. RBAC 要解决 ...

  8. 完美兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面js代码

    <script  type="text/javascript"> //设为首页 function SetHome(obj,url){     try{         ...

  9. POJ1286 Necklace of Beads

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8263   Accepted: 3452 Description Beads ...

  10. python3 购物车练习

    # 购物车# 功能要求:# 要求用户输入总资产,例如:2000# 显示商品列表,让用户根据序号选择商品,加入购物车# 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功.# 可充值.某商品 ...