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. Go安装gRPC

    grpc-go的官方安装命令 go get google.golang.org/grpc 无法正常使用. 我们可以用以下的命令替代,达到同样的效果 git clone https://github.c ...

  2. iOS开发架构学习记录

    闲着没事看了一些iOS开发架构的视频,简单的介绍了几个常用的架构设计,现将它记录如下,以后有时间再专门写这方面的内容,大家可以看看,感兴趣的就进一步学习. 一.架构基础 1.架构设计的目的 进一步解耦 ...

  3. 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)

    摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...

  4. Java 代码中如何调用 第三方Api

    在代码中调用第三方API 获取数据 package com.example.demo.utils; import com.alibaba.fastjson.JSONObject; import lom ...

  5. 【原】nginx配置文件

    一:下载nginx方式 1.yum install nginx 2.源码安装 二:学习网址 nginx documentation — DevDocs 三:配置文件信息 server { listen ...

  6. docker的私有化仓库harbor搭建

    目前比较流行的docker私有化仓库是harbor,harbor是一个github开源的项目,直接在github上搜索即可,下载地址:https://github.com/goharbor/harbo ...

  7. VUE引入模块之import xxx from 'xxx' 和 import {xxx} from 'xxx'的区别

    import FunName from ‘../xxx’ export defualt function FunName() { return fetch({ url: '/article/list' ...

  8. Linux下杀掉所有得java进程

    --转自https://blog.csdn.net/oppo62258801/article/details/81434038 1.Linux查看所有Java进程 ps -ef | grep java ...

  9. 理解JS中的回调(Callback)函数

    今天写代码时写了一个函数,想实现Nodejs查询pgSQL的数据查出来并把结果作为返回值,结果发现拿不到这个值,查了下资料才恍然大悟,这是Nodejs的最大特性--非阻塞! 查询数据操作作为比较消耗资 ...

  10. Spring 注意事项

    1.在我们使用spring 5.x版本的时候,要求junit 的jar版本是4.12及以上. 2.不管是什么样的配置,当发现之前能用,改了位置就不能用的时候,首先要考虑的问题就是:是否有约束上顺序的要 ...