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. 记忆化搜索 || POJ 1088 滑雪

    从任意一点可以往上下左右比它小的数那里走,问最远长度是多少 *解法:每一点dfs搜索一遍 记忆化搜索:http://blog.csdn.net/acmer_sly/article/details/53 ...

  2. log4j.xml 精选的log4j.xml文档,比较详细,网上的版本很多,这个版本相对而言比较完整

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUB ...

  3. Linux-fuser

    Linux-fuser 1. 描述 2. 选项3. EXAMPLES4. RESTRICTIONS 限制5. SIGNAL 可用信号 fuser - 使用文件或套接字识别进程 1. 描述 fuser使 ...

  4. Android Studio中删除无效的字符串资源

    1.定位到当前项目中的strings.xml文件 1.菜单栏找到"Analyze"->"Run Inspection By Name..."->输入 ...

  5. TWaver可视化编辑器的前世今生(一)电信网管编辑器

    走到今天,TWaver,一个致力于在技术领域(Technology)的弄潮儿(Waver),已经是第十二个年头.当年网吧的小网管都是IDC机房的运维人员了,TWaver也见证了这个时代的成长变迁. 萌 ...

  6. JavaScript设计模式基础之面向对象的JavaScript(二)

    多态 多态的实际含义:同一操作作用与不同的对象上面,可以产生不同的解释和不同的执行结果,就是说,给不同的对象发送同一个消息 的时候,这些对象会根据这个消息分别给出不同的反馈 代码如下: class D ...

  7. luogu P1205 方块转换

    题目描述 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式: 1:转90度:图案按顺时针 ...

  8. php S3

    转载自:http://www.cnblogs.com/wangxusummer/p/6398772.html <?php /* * To change this license header, ...

  9. pandas.read_csv 报ssl.SSLError

  10. Images for Journals

    Images for publication Table of Contents 1. Images for publication 1.1. image format : vector image ...