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

思路:简单的最小生成树板子题(prim)
const int maxm = ;

struct Node {
int u, v, w;
Node(int _u, int _v, int _w):u(_u), v(_v), w(_w){}
bool operator<(const Node &a)const{
return a.w < w;
}
}; vector<Node> Edge;
vector<int> G[maxm]; void addedge(int u, int v, int w) {
Edge.push_back(Node(u, v, w));
G[u].push_back(Edge.size() - );
} int N, vis[maxm]; void init() {
for(int i = ; i <= N; ++i)
G[i].clear();
memset(vis, , sizeof(vis));
Edge.clear();
} int main() {
while(scanf("%d", &N) != EOF) {
init();
int val, ans, siz, times;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
scanf("%d", &val);
if(i != j)
addedge(i, j, val);
}
ans = times = ;
vis[] = ;
priority_queue<Node> q;
siz = G[].size();
for(int i = ; i < siz; ++i) {
int num = G[][i];
q.push(Node(, Edge[num].v, Edge[num].w));
}
while(!q.empty() && times < N) {
Node now = q.top();
q.pop();
int u = now.v;
if(vis[u]++) continue;
ans += now.w;
siz = G[u].size();
times++;
for(int i = ; i < siz; ++i) {
int num = G[u][i];
if(!vis[Edge[num].v])
q.push(Node(u, Edge[num].v, Edge[num].w));
}
}
printf("%d\n", ans);
}
return ;
}

 

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

  1. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  4. POJ 1258

    http://poj.org/problem?id=1258 今天晚上随便找了两道题,没想到两道都是我第一次碰到的类型———最小生成树.我以前并没有见过,也不知道怎么做,然后就看书,思路很容易理解 但 ...

  5. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

  6. POJ 1258 Agri-Net(Prim算法求解MST)

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

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

    链接: http://poj.org/problem?id=1258 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...

  8. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  9. poj 1258 Agri-Net 解题报告

    题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...

随机推荐

  1. NSObject类的API介绍

    这篇文章围绕的对象就是NSObject.h文件,对声明文件中的属性.方法进行必要的“翻译”. 该文件大致由两部分组成:NSObject协议和NSObject类. (一)NSObject协议 - (BO ...

  2. poj1988 Cube Stacking(并查集

    题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[ ...

  3. nginx 反向代理memcached、websocket及nginx文件方面的优化

    安装memcached服务,并启动添加数据 yum -y install memcached systemctl start memcached.service 启动 [root@python ~]# ...

  4. day4-2数组及方法

    数组: Js数组 可以存放任意数据类型的数据 如果索引大于数组的长度,数组自动增加到该索引值加1的长度 var arr = ["terry","larry",& ...

  5. js--滑动块

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 用C/C++创建windows服务程序

    转载:https://blog.csdn.net/chenyujing1234/article/details/8023816 一.演示过程下方代码演示了如何使用vs(C/C++)创建windows服 ...

  7. Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

    Springboot + redis + 注解 + 拦截器来实现接口幂等性校验   1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...

  8. 项目根目录下.gitignore

    7.项目根目录下.gitignore  # 此为注释 – 将被 Git 忽略  *.a # 忽略所有 .a 结尾的文件  !lib.a # 但 lib.a 除外  /TODO # 仅仅忽略项目根目录下 ...

  9. 【剑指Offer面试编程题】题目1370:数组中出现次数超过一半的数字--九度OJ

    题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...

  10. ubuntu16.04 使用tensorflow object detection训练自己的模型

    一.构建自己的数据集 1.格式必须为jpg.jpeg或png. 2.在models/research/object_detection文件夹下创建images文件夹,在images文件夹下创建trai ...