【内含最小生成树Prim模板】

题目https://www.luogu.org/problemnew/show/P1546

题意:给定一个邻接矩阵。求最小生成树。

思路:点少边多用Prim。

Prim其实是把已经在最小生成树里的节点缩成一个,用priorityqueue每次找到距离当前最小生成树距离最小的那条边,加入树中。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n;
const int maxn = ;
int head[maxn], tot = ;
struct edge{
int to, nxt, w;
}e[maxn * maxn]; void add(int x, int y, int w)
{
e[++tot].to = y;
e[tot].w = w;
e[tot].nxt = head[x];
head[x] = tot;
// e[++tot].to = x;
// e[tot].w = w;
// e[tot].nxt = head[y];
// head[y] = tot;
} int ans = ;
int d[maxn];
bool vis[maxn];
int prim(int s)
{
priority_queue<pr, vector<pr>, greater<pr> >que;
memset(d, 0x3f, sizeof(d));
int num = ;
vis[s] = true;
for(int i = head[s]; i; i = e[i].nxt){
if(e[i].to != ){
que.push(make_pair(e[i].w, e[i].to));
d[e[i].to] = min(d[e[i].to], e[i].w);
}
}
while(!que.empty() && num != n){
while(!que.empty() && vis[que.top().second])que.pop();
if(que.empty())break;
int x = que.top().second;
vis[x] = true;
ans += que.top().first;que.pop();
num++;
for(int i = head[x]; i; i = e[i].nxt){
if(!vis[e[i].to] && d[e[i].to] > e[i].w){
que.push(make_pair(e[i].w, e[i].to));
d[e[i].to] = e[i].w;
}
}
}
if(num != n)return -;
else return ans;
} int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
int w;
scanf("%d", &w);
if(i != j){
add(i, j, w);
}
}
}
printf("%d\n", prim());
}

洛谷1546 最短网络Agri-Net【最小生成树】【prim】的更多相关文章

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

    洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...

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

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

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

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

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

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

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

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

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

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

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

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

  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. Azure经典虚拟机(Windows)如何监测单个磁盘的使用空间

    Azure云平台创建经典虚拟机(Windows)后,发现仪表板的监测项下默认是没有针对磁盘空间进行检测的指标的 本地机器安装Windows Azure Powershell模块,并通过如下命令登陆并查 ...

  2. c# 面向对象/继承关系设计

    继承 RTTI RTTI 概念 RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. ...

  3. java日志框架系列(9):logback框架过滤器(filter)详解

    过滤器放在了logback-classic模块中. 1.logback-classic模块中过滤器 分类(2种):常规过滤器.TurboFilter过滤器. 1.常规过滤器 常规过滤器可以通过自定义进 ...

  4. 关于Django ModelForm渲染时间格式问题

    关于Django ModelForm渲染时间格式问题 直接定义DateTimeInput或者DateTimeFile是不行的,渲染在html页面中的仍然是Input text类型 解决办法:自定义小部 ...

  5. chrome中显示DNS_PROBE_FINISHED_NO_INTERNET无法上网,但是IE可以上

    以管理员方式运行cmd,执行如下命令 ipconfig /release ipconfig /all ipconfig /flushdns ipconfig /renew netsh int ip s ...

  6. pandas 索引笔记

    import pandas as pd import numpy as np s = pd.Series(np.random.rand(5), index=list('abcde')) # 创建序列, ...

  7. 【规律】Cunning Friends

    Cunning Friends 题目描述 Anthony and his friends Ben and Chris decided to play a game. They have N piles ...

  8. C#基础--Ref与Out区别

    两者都是按地址传递的,使用后都将改变原来参数的数值. class Program { static void Main(string[] args) { int num = 1; Method(ref ...

  9. Linux 命令行:cURL 的十种常见用法

    Linux 命令行:cURL 的十种常见用法 文章目录 1. 获取页面内容 2. 显示 HTTP 头 3. 将链接保存到文件 4. 同时下载多个文件 5. 使用 -L 跟随链接重定向 6. 使用 -A ...

  10. springboot启动流程(八)ioc容器refresh过程(下篇)

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 上一篇文章,我们知道了解析过程将从解析main方法所在的主类开始.在文章的最后我们稍微看 ...