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;
}

POJ:1258-Agri-Net的更多相关文章

  1. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

  2. POJ:1182 食物链(带权并查集)

    http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...

  3. POJ:2229-Sumsets(完全背包的优化)

    题目链接:http://poj.org/problem?id=2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissio ...

  4. POJ:3126-Prime Path

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...

  5. POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)

    原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...

  6. POJ :3614-Sunscreen

    传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...

  7. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  8. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  9. POJ:1017-Packets(贪心+模拟,神烦)

    传送门:http://poj.org/problem?id=1017 Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...

  10. POJ:3190-Stall Reservations

    传送门:http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total ...

随机推荐

  1. 超详细Hexo+Github博客搭建小白教程

    原文链接:超详细Hexo+Github博客搭建小白教程 去年9月的时候开始搭建了第一个自己的独立博客,到现在也稍微像模像样了.很多小伙伴应该也想过搭建一个自己的博客,网上也有一堆详细教程.我在此稍稍总 ...

  2. Unity利用AnimationCurve做物体的各种运动

    ​之前一直都是自己学习Unity各种做Demo,最近开始正式使用Unity来做一个款2d的游戏. 其中在做一个类似小球弹跳运动的时候遇到了点问题,查找了很多资料,无意间发现AnimationCurve ...

  3. Linux命令之查看服务进程(ps aux、ps -aux、ps -ef)的运用

    执行ps命令即可列出的是当前服务器进程的快照(时间点),如果想要实时动态的显示进程信息,就可以使用top命令. linux上进程有5种状态:  1. 运行(正在运行或在运行队列中等待)  2. 中断( ...

  4. 51Nod 1600 Simple KMP SAM+LCT/树链剖分

    1600 Simple KMP 对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i)显然对于一个字符串,如果我们将每个0<= ...

  5. cesium 显示视角高度以及鼠标经纬度

    HTML中的内容 <div id="cesiumContainer"> <!-- 设置经纬度显示 --> <span style="font ...

  6. IOS 加载更多数据中

    /**点击添加*/ - (IBAction)loadBtnClick { //1.隐藏加载按钮 self.loadBtn.hidden=YES; //2.显示“正在加载” self.loadingVi ...

  7. UESTC 761 LoveZx与期末考试

    被卡的一道题,其他情况都想出来了,主要是没想好A[i] == B[j]时候的处理,取最后面最大的可能不是最优解,相等的时候我暴力比较后缀的(为此还要维护一个链),这个操作是O(len) 所以T了.(也 ...

  8. Android(java)学习笔记64:Android权限大全

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permissio ...

  9. 软件的依赖关系:类图关系:is-a has-a use-a

    基本描述: 类图关系:is-a has-a use-a: 依赖规则: 依赖倒置: 依赖注入: 接口隔离: 无环依赖: 稳定依赖: 依赖倒置描述: 底层抽象-->高层抽象 ^          ^ ...

  10. Map的嵌套,HDU(1263)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 新学的map的嵌套 #include <stdio.h> #include < ...