Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44032   Accepted: 18001

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

Source

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

#include<stdio.h>
#include<string.h>
int map[105][105];
int dis[105],vis[105];
int n;
#define inf 999999999
int prim(int u){
int sum=0;
for(int i=1;i<=n;i++){
dis[i]=map[u][i];
}
vis[u]=1;
for(int i=1;i<n;i++){
int tmin=inf;
int temp;
for(int j=1;j<=n;j++){
if(dis[j]<tmin&&!vis[j]){
tmin=dis[j];
temp=j;
}
}
sum+=tmin;
vis[temp]=1;
for(int k=1;k<=n;k++){
if(dis[k]>map[temp][k]&&!vis[k])
dis[k]=map[temp][k];
}
}
return sum;
}
int main(){
while(scanf("%d",&n)!=EOF){
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
printf("%d\n",prim(1));

}
return 0;
}

poj1258 Agri-Net 最小生成树的更多相关文章

  1. Poj1258 Agri-Net (最小生成树 Prim算法 模板题)

    题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  2. POJ1258 Agri-Net【最小生成树】

    题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离. 思路: 又是一个最小生成树,因为 ...

  3. POJ1258 Agri-Net MST最小生成树题解

    搭建一个最小代价的网络,最原始的最小生成树的应用. 这里使用Union find和Kruskal算法求解. 注意: 1 给出的数据是原始的矩阵图,可是须要转化为边表示的图,方便运用Kruskal,由于 ...

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

    Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...

  5. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  6. POJ1258 基础最小生成树

    本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一 ...

  7. 最小生成树Prim poj1258 poj2485 poj1789

    poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u ...

  8. POJ1258:Agri-Net(最小生成树模板题)

    http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...

  9. POJ1258 (最小生成树prim)

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

  10. POJ1258最小生成树简单题

    题意:       给你个图,让你求一颗最小生成树. 思路:      裸题,克鲁斯卡尔或者普利姆都行. #include<stdio.h> #include<algorithm&g ...

随机推荐

  1. android常用的弹出提示框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  2. unicode和gbk的互相转换

    unicode和gbk的互相转换主要依靠window下的escape和unescape方法,然后把%u替换成\u就好了; var GB2312UnicodeConverter = { ToUnicod ...

  3. Spring 常用注解

    用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册 <context:component-scan base-package=”pagkage1[, ...

  4. 【CodeForces 625A】Guest From the Past

    题 题意 一升奶可以花费a元,也可以话b元买然后获得c元,一开始有n元,求最多买多少升奶. 分析 贪心,如果b-c<a,且n≥b,那就买b元的,n先减去b再看看够买多少瓶,然后再+1瓶,余款再购 ...

  5. 【CodeForces 618B】Guess the Permutation

    题 题意 有个1到n的一个全排列,告诉你第i个数和全部n个数相比的较小的是哪个,和自己相比时为0,于是有个主对角线为0的矩阵,求原数列 分析 我的想法是,给我们的每一行之和按大小排一下,就知道第i个数 ...

  6. MySQL tips (日期时间操作/concat 等)

    1.  Query结尾要加一个分号: 2.  数据库和表 SHOW DATABASES;    USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...

  7. 和声搜索算法-python实现

    HSIndividual.py import numpy as np import ObjFunction class HSIndividual: ''' individual of harmony ...

  8. tomcat7登录账户配置

    tomcat7和tomcat6的用户信息配置有些不一样,tomcat7中添加了manager=gui和admin-gui角色,配置参考如下: 再 tomcat 文件夹的conf文件夹中的 tomcat ...

  9. NSThread - (void)start vs java Thread implements Runnable

    This method spawns the new thread and invokes the receiver’s main method on the new thread. If you i ...

  10. MyISAM和InnoDB的索引在实现上的不同

    1 MyISAM只把索引载入内存,数据缓存依赖于操作系统,InnoDB把索引和数据都载入内存缓冲 2 MyISAM数据库中的数据是按照插入的顺序保存,在每个索引节点中保存对应的数据行的地址,理论上说主 ...