洛谷P1546 最短网络 Agri-Net

最小生成树模板题。

直接使用 Kruskal 求解。

复杂度为 \(O(E\log E)\) 。

#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std; const int maxn = 105 * 105;
int n, x, tot, ans, f[105];
struct edge{
int from, to, w;
bool operator < (const edge & _edge) const {
return w < _edge.w;
}
}e[maxn]; void init(){
for(int i = 1; i <= n; i++) f[i] = i;
}
int father(int x){
if(f[x] != x){
f[x] = father(f[x]);
}
return f[x];
}
void _union(int a, int b){
int fa = father(a), fb = father(b);
f[fa] = f[fb];
}
int ok(int a, int b){
int fa = father(a), fb = father(b);
return fa == fb ? 1 : 0;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
scanf("%d", &x);
if(i < j){
tot++;
e[tot].from = i; e[tot].to = j; e[tot].w = x;
}
}
}
init();
int m = n * (n - 1) / 2;
int cnt = 0;
sort(e + 1, e + 1 + m);
for(int i = 1; i <= m; i++){
if(!ok(e[i].from, e[i].to)){
ans += e[i].w;
_union(e[i].from, e[i].to);
cnt++;
if(cnt == n - 1) break;
}
}
printf("%d\n", ans);
return 0;
}

洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)的更多相关文章

  1. 洛谷 P1546 最短网络 Agri-Net(最小生成树)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P1546 首先不难看出这道题的思想是用了最小生成树,但是这道题有难点: 1.读题读不明白 2.不会读入 ...

  2. 洛谷 P1546 最短网络 Agri-Net

    题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...

  3. 洛谷P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...

  4. 洛谷——P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...

  5. 洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树

    题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...

  6. 洛谷 P1546 最短网络 Agri-Net(最小生成树)

    题目链接 https://www.luogu.org/problemnew/show/P1546 说过了不复制内容了 显然是个最小生成树. 解题思路 prim算法 Kruskal算法 prim算法很直 ...

  7. 洛谷1546 最短网络Agri-Net【最小生成树】【prim】

    [内含最小生成树Prim模板] 题目:https://www.luogu.org/problemnew/show/P1546 题意:给定一个邻接矩阵.求最小生成树. 思路:点少边多用Prim. Pri ...

  8. 洛谷 P1546 最短网络 Agri-Net x

    题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...

  9. 洛谷P1546 最短网络 Agri-Net(Prim堆优化)

    #include<bits/stdc++.h> using namespace std; ; const int INF=0x3f3f3f3f; inline void read(int ...

随机推荐

  1. SVN检出新项目

    1.新建文件夹SourseCode -->打开SourseCode文件夹,右键空白处 ---> 选择SVN Checkout --选择URL of repository,选择Checkou ...

  2. VMware下Linux构建仅主机模式的局域网网络配置方案

    最近使用Linux,进行网络配置,以前都是桥接直连,然后直接组网.由于一些原因现在虚拟机做内网使用,不用上网,只能使用仅主机模式.在仅主机模式下进行虚拟机组网. 仅主机模式下各个虚拟机只能和主机通信, ...

  3. tomcat 端口8080占用问题

    启动tomcat时,有时会出现8080端口占用的问题. 解决方法: 终端:ps -e | grep tomcat 会看到下边的结果 途中标记的是进程号,kill掉即可. kill -9 9734(97 ...

  4. MySQL的练习

    mysql登录:方法1:使用Command Line Client登录,缺点:不显示报错信息 方法2:使用cmd登录(cmd的常用:查看ip地址:ipcongfi-----定时关机:shutdown ...

  5. Sql 语句中使用参数

    using System; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication2 { public ...

  6. 学Python的第五天

    最近忙着学MySQL,但是小编也不会放弃学Python!!! 因为热爱所以学习~ 好了各位,进入正题,由于时间问题今天学的不是很多.... #!/usr/bin/env python # -*- co ...

  7. python函数传参和返回值注意事项

    函数传参 空参数 定义函数时括号里面没有形参,调用时不用传参. def func(): print('null para.') # 调用 func() 位置传参 规定形参的数量,调用时必须传递相同数量 ...

  8. shell脚本--expect自动应答

    expect自动应答  TCL语言 需求1:远程登录到A主机,什么事情也不做 #! /usr/bin/env expect # 开启一个程序 spawn ssh root@192.144.213.11 ...

  9. css3 first-of-type选择器以及css3选择器中:first-child与:first-of-type的区别

    CSS3 first-of-type选择器 “:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子 ...

  10. 使用Github 当作自己个人博客的图床

    使用Github 当作自己个人博客的图床 前提 本文前提: 我个人博客的草稿是存放在 github上的一个仓库 diarynote 截图存放的图片或者需要放在文章中图片,会固定存放在对应的文件夹中,我 ...