Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44487   Accepted: 18173

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算法求解就可以。
代码:
/*
prim
Memory 204K
Time 16MS
*/
#include <iostream>
#include<cstdio>
using namespace std;
#define MAXV 101
#define inf 1<<29 int map[MAXV][MAXV],n; void prim()
{
int i,j,dis[MAXV],vis[MAXV],mi,v;
for(i=1;i<=n;i++)
{
dis[i]=map[1][i];
vis[i]=0;
}
for(i=1;i<=n;i++)
{
mi=inf;
for(j=1;j<=n;j++)
if(!vis[j] && dis[j]<mi)
{
mi=dis[j];
v=j;
} vis[v]=1;
for(j=1;j<=n;j++)
if(!vis[j] && dis[j]>map[v][j])
dis[j]=map[v][j];
}
int sum=0;
for(i=1;i<=n;i++)
sum+=dis[i]; printf("%d\n",sum);
} int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
prim();
}
return 0;
}







poj 1258 Agri-Net(Prim)(基础)的更多相关文章

  1. POJ 1258 Agri-Net(Prim)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  2. poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0

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

  3. POJ 1258 Agri-Net(Prim算法)

    题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...

  4. POJ 1258 Agri-Net (最小生成树+Prim)

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

  5. POJ 1258:Agri-Net Prim最小生成树模板题

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

  6. POJ 1258 Agri-Net (prim水题)

    Agri-Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Subm ...

  7. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  8. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  9. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

随机推荐

  1. excel数据比对,查找差异

    1.选中需比对的数据 2.开始->条件格式->突出显示单元格规则->重复值 3.选择唯一值,点击确定 4.结果展示 5.颜色标识的即:不同值

  2. 2018 CCPC 女生赛 hdoj6288 缺失的数据范围

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6288 Summarize:1.二分查找答案: 2.自带log函数精度不够,需自己写: 3.注意二分递归 ...

  3. C++学习周记

    自开学到现在,原本可谓是对C++一无所知,也通过这几周的学习而渐渐有所了解. 最开始的编程任务虽然简单,但解决过程中却不乏磕绊,由一开始的中英文字符的不注意,到现在对一些函数的运用难免出错,出现bug ...

  4. 编写一个微信小程序

    1.创建项目 2.创建目录及文件,结构如下:

  5. 解决sqlplus无法退格问题

      # wget http://download.openpkg.org/components/cache/rlwrap/rlwrap-0.42.tar.gz # tar -zxf rlwrap-0. ...

  6. Django框架基础知识11-会话状态保持及表单

    浏览器存储cookie的方式不太安全,那有没有更好些的来存储登入状态的方式呢??? 状态保持----cookie和session: 状态保持: 1.http协议是无状态的:每次请求都是一次新的请求,不 ...

  7. Linux项目发布流程

    Linux项目发布流程(一) 1.安装pyhton3.7 的依赖包 yum -y groupinstall "Development tools" yum -y install z ...

  8. java多线程之ForkJoinPool

    转https://www.cnblogs.com/lixuwu/p/7979480.html 阅读目录 使用 背景:ForkJoinPool的优势在于,可以充分利用多cpu,多核cpu的优势,把一个任 ...

  9. 【HIHOCODER 1576】 子树中的最小权值(线段树维护DFS序)

    描述 给定一棵N个节点的树,编号1~N.其中1号节点是根,并且第i个节点的权值是Vi. 针对这棵树,小Hi会询问小Ho一系列问题.每次小Hi会指定一个节点x,询问小Ho以x为根的子树中,最小的权值是多 ...

  10. Insert or Merge

    7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...