Agri-Net

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 65322 Accepted: 27029

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


解题心得:

  1. 一个裸的最小生成树,只不过给你的距离表达式是个矩阵。

#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 110; int n,father[maxn],tot;
struct Path {
int s,e,len;
}path[maxn*maxn]; int find(int x) {
if(x == father[x])
return x;
return father[x] = find(father[x]);
} void merge(int x,int y) {
int fx = find(x);
int fy = find(y);
father[fx] = fy;
} bool cmp(Path a,Path b) {
return a.len < b.len;
} void init() {
tot = 0;
for(int i=0;i<n;i++) {
father[i] = i;
for (int j = 0; j < n; j++) {
int temp;
scanf("%d", &temp);
path[tot].s = i;
path[tot].e = j;
path[tot++].len = temp;
}
}
sort(path,path+tot,cmp);
} int main() {
while(scanf("%d",&n) != EOF) {
init();
int ans = 0;
for(int i=0;i<tot;i++){
if(find(path[i].s) != find(path[i].e)) {
ans += path[i].len;
merge(path[i].s,path[i].e);
}
}
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. Java 8中一些常用的全新的函数式接口
  2. CNI插件源码示例,对于github.com/rajatchopra/ocicni库的分析
  3. EventBus的使用
  4. 按照需要分别率长宽比导出图片(python 3)
  5. 【ntp】centos7下ntp服务器设置
  6. IE10、IE11出现“__doPostBack未定义”的解决办法。
  7. android 数字键盘制作
  8. R学习日记——分解时间序列(季节性数据)
  9. Sass@规则
  10. elasticsearh 中每个节点中需要有相同的插件
  11. Splay POJ3468(老题新做)
  12. UVa/数组和字符串习题集
  13. EMC题
  14. PHP处理XML文档,没有CDATA部分数据处理
  15. angular2监听页面大小变化
  16. xshell 禁用铃声 提示音
  17. 自然语言交流系统 phxnet团队 创新实训 项目博客 (四)
  18. GetLastError函数
  19. 3层+SVN学习笔记(1)
  20. 【转】socket 通信简介

热门文章

  1. Django Rest Framework进阶二
  2. 使用跨平台图表控件TeeChart如何从DAT或TEXT文件中导入数据
  3. PyYAML使用
  4. js中字符串怎么转化为日期
  5. 浩顺晶密K-5 打卡时间设置
  6. Installing xgboost and cmake, mingw64 and mingw
  7. 模拟猜数(POJ2328)
  8. 2018.7.23 oracle中的CLOB数据类型
  9. Java解析Excel工具类(兼容xls和xlsx)
  10. An error occurred at line: 1 in the generated java file问题处理