题干:

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

解题报告:

最小生成树裸题。

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;int n,m;
int top,f[105];
struct Edge {int u,v,w;Edge(){}Edge(int u,int v,int w):u(u),v(v),w(w){}
} e[100 * 99];
int getf(int u) {return f[u]==u?u:f[u]=getf(f[u]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) f[t2]=t1;
}
bool cmp(Edge a,Edge b) {return a.w < b.w;
}
int main()
{int val;while(~scanf("%d",&n)) {top = 0;for(int i = 1; i<=n; i++) {f[i]=i;for(int j = 1; j<=n; j++) {scanf("%d",&val);if(i!=j) e[++top] = Edge(i,j,val);}}sort(e+1,e+top+1,cmp);m=0;val = 0;for(int i = 1; i<=top; i++) {if(getf(e[i].u) != getf(e[i].v)) {m++;merge(e[i].u,e[i].v);val += e[i].w;}if(m == n-1) break;}printf("%d\n",val);}   return 0;
}

总结:其实那个 i != j可以改成i<j,会稍微快一点,不过也无所谓都可以ac,一个6ms,一个5ms。

【OpenJ_Bailian - 1258】【POJ - 1258】Agri-Net (最小生成树裸题)相关推荐

  1. 【HDU - 1102】Constructing Roads (最小生成树裸题模板)

    题干: There are N villages, which are numbered from 1 to N, and you should build some roads such that ...

  2. CSP认证201412-4 最优灌溉[C++题解]:最小生成树裸题、Kruskal算法求最小生成树

    题目分析 来源:acwing 分析:这是一道最小生成树的裸题. 这里默写Kruskal求最小生成树的最小费用的模板. 最小生成树模板请参考笔者的另一篇博文: 最小生成树板子-AcWing 859. K ...

  3. BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2925  Solved: 1927 [Submit][S ...

  4. 惠民工程(最小生成树裸题)

    惠民工程 Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status Descripti ...

  5. 最小生成树模板 POJ——1258

    最小生成树是一个比较简单数据结构,形成最小生成树的方式有两种. 最小生成树是有图生成树,保证树的每条边的权值之和最小的生成树就叫做最小生成树,这一类的题目起初比较基础,主要是熟悉模板,POJ 1258 ...

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

    题目: Description Farmer John has been elected mayor of his town! One of his campaign promises was to ...

  7. 【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)

    题干: Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to ...

  8. POJ1258最小生成树简单题

    题意:       给你个图,让你求一颗最小生成树. 思路:      裸题,克鲁斯卡尔或者普利姆都行. #include<stdio.h> #include<algorithm&g ...

  9. 最大流问题模板-java-蓝桥杯-算法训练-网络流裸题

    1. 本题相关资料 题目链接 这是一道求最大流的模板题. 最大流预备知识:dfs,bfs,图论基础(图的存储). 网上已经有很多最大流的解析,我就不再唠叨了,还不了解的同学可参考这个博客:什么是最大流 ...

最新文章

  1. workbench应力应变曲线_说说真应力真应变
  2. java url 拦截_Spring mvc设置某些url不被interceptor拦截器拦截的方法
  3. Linux密码文件passwd和shadow分析
  4. [转]基于overlayfs的硬盘资源隔离工具troot
  5. C++(STL):02---tuple容器
  6. .h 与 .hpp 文件
  7. 爬虫爬取车主指南各类汽车数据
  8. Redis下载部署配置运行及修改登录地址、端口和密码
  9. 对抗神经网络(一)——GAN
  10. 玩转数据可视化之R语言ggplot2:(四)单一基础几何图形绘制
  11. Stronger Data Poisoning Attacks Break Data Sanitization Defenses
  12. OpenCV:imwrite存储图像后为全黑
  13. 企业如何搭建呼叫中心?
  14. 分享6个2020年对我帮助很大的软件
  15. 3DMatch数据集
  16. 手机群控系统(补充篇)
  17. Watson出现误诊遭质疑,智能医疗到底靠不靠谱
  18. Themida是否支持PowerBuilder应用程序的保护?—— Themida常见问答集锦
  19. 初识python五大特点
  20. 智能运维监控系统怎么选

热门文章

  1. CodeForces 799B (B) T-shirt buying
  2. 并行DA实验c语言程序,哈工大C语言DA实验报告.doc
  3. 5d4的白平衡模式_佳能5D4/5D3/6D2系列中高端单反相机和全画幅微单EOS R专题系列课程...
  4. python 修改图片尺寸_python 批量修改图片大小
  5. Vim案列掌握vim的基本操作——案例一:vim创建编写txt文件
  6. java类与对象实验报告心得体会_第四周课程总结与实验报告(Java简单类与对象)...
  7. 张掖计算机职称考试,甘肃省张掖市2018年3月计算机等级考试公告
  8. 2020数学建模国赛C题思路
  9. 微型计算机在温室管理中的应用初探,文献综述-测控051-陈杰.doc
  10. 网络资产管理系统_固定资产管理系统的常用操作