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. C++ Knowledge series STL & Const

    Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...

  2. [SVN]TortoiseSVN工具培训4─客户端常用操作命令

    1.权限认证 当进行SVN任何操作时,如果是首次操作,SVN会弹出权限认证. 输入用户名和密码点击确认即可完成认证. 勾选保存用户数据信息,可以避免将来重复输入用户名和密码认证. 2.删除权限认证信息 ...

  3. artTemplate教程

    artTemplate教程 官方文档 一个简单的例子 <!DOCTYPE html> <html lang="zh"> <head> <m ...

  4. python_opencv应用系列1:图片读写

    opencv的读写非常简单,主要用到的就是imread和imwrite两个函数 读取图片示例 import cv2 #imread(filename[, flags]) -> retval im ...

  5. framework7 1.3.5 路由跳转后DOM失效问题

    再这个版本的7会存在一个问题,那就是loadpage到指定页面后才做其中的DOM比如DIV里面的text或者HTML,虽然控制台会显示改变后的值但是页面上却还是原值,这时候需要改变方法使用reload ...

  6. POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)

    题目链接: https://vjudge.net/problem/POJ-2456 题目大意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 ...

  7. POJ-1840 Eqs---二分

    题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50]  自变量xi∈[ ...

  8. python Unittest+excel+ddt数据驱动测试

    #!user/bin/env python # coding=utf- # @Author : Dang # @Time : // : # @Email : @qq.com # @File : # @ ...

  9. IIS 处理程序“PageHandlerFactory-Integrated”

    出现这种错误是因为先安装了.net framework 4.0然后才安装了iis,此种情况下iis默认只支持.net framewrok 2.0,要解决此问题,需要在iis中注册.net framew ...

  10. google detection

    paper: Scalable, High-Quality Object Detection ILSVRC14上,detection刷到55.7%的MAP,google插入的地方,别人倍感压力啊. 总 ...