Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44373   Accepted: 18127

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
 #include <stdio.h>
#define INF 100000
int n;
int map[][];
int dis[],vis[];
int Prim(){
for(int i=;i<n;i++){
vis[i]=;
dis[i]=INF;
}
dis[]=; for(int i=;i<n;i++){
int min=INF;
int p;
for(int j=;j<n;j++){
if(!vis[j]&&min>dis[j]){
min=dis[j];
p=j;
}
}
vis[p]=;
for(int j=;j<n;j++){
if(!vis[j]&&dis[j]>map[p][j]){
dis[j]=map[p][j];
}
}
}
for(int i=;i<n;i++){
dis[]+=dis[i];
}
return dis[];
}
int main() {
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&map[i][j]);
}
}
int min=Prim();
printf("%d\n",min);
}
return ;
}

Agri-Net - poj 1258 (Prim 算法)的更多相关文章

  1. Highways - poj 2485 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description T ...

  2. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  3. Agri-Net POJ - 1258 prim

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; #defi ...

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

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

  5. POJ 1258 Agri-Net(Prim)

    题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

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

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  7. POJ 1258 Agri-Net (Prim&Kruskal)

    题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...

  8. POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)

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

  9. poj 1258 Agri-Net(Prim)(基础)

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

随机推荐

  1. Codeforces 908 D New Year and Arbitrary Arrangement

    Discription You are given three integers k, pa and pb. You will construct a sequence with the follow ...

  2. Uprobes: userspace probes >= Linux 3.5

    https://lwn.net/Articles/499190/ https://github.com/andreoli/fulltrace Prominent features in Linux 3 ...

  3. 使用 SQL Server 的 uniqueidentifier 字段类型

    原文:使用 SQL Server 的 uniqueidentifier 字段类型 SQL Server 自 2008 版起引入了 uniqueidentifier 字段,它存储的是一个 UUID, 或 ...

  4. 一次快速改写 SQL Server 高效查询的范例

    原文:一次快速改写 SQL Server 高效查询的范例 最近線上系統突然出現匯出資料超過 10 筆時,查詢逾時的狀況,在仔細查找之後. 發現了問題原因,透過應用端與數據端兩邊同時調整,將查詢的效率提 ...

  5. 彻底理解Javascript 中的 Promise(-------------------------------***---------------------------------)

    ES6原生提供了 Promise 对象. 到底是何方妖怪呢?打出来看看: 所谓 Promise,就是一个对象,用来传递异步操作的消息.它代表了某个未来才会知道结果的事件(通常是一个异步操作),并且这个 ...

  6. kubernetes1.5.2--部署监控服务

    本文基于kubernetes 1.5.2版本编写 Heapster是kubernetes集群监控工具.在1.2的时候,kubernetes的监控需要在node节点上运行cAdvisor作为agent收 ...

  7. ADOX创建ACCESS数据库列名的数据类型

    Type   属性             指示   Parameter.Field   或   Property   对象的操作类型或数据类型. 设置和返回值 设置或返回下列   DataTypeE ...

  8. 【Hadoop】HADOOP 总结--思维导图

  9. 2017.7.7 postgreSQL在插入造成重复时执行更新

    参考来自:https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/1109198#11 ...

  10. hdu 4630 No Pain No Game(线段树+离线操作)

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...