【内含最小生成树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. springboot集成elk 四:springboot + Elasticsearch+Jest

    依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  2. 图数据库neo4j添加算法包

    1. 从https://github.com/neo4j-contrib/neo4j-graph-algorithms/releases下载相应版本jar包,放到 C:\Users\Administr ...

  3. 分布式缓存 - hash环/一致性hash

    一 引言 当前memcached,redis这类分布式kv缓存已经非常普遍.我们知道memcached的分布式其实是一种"伪分布式",也就是它的服务器节点之间其实是无关联的,之间没 ...

  4. homestead的创建和使用

    1.下载vistualbox和vagrant并安装 2.安装了git的话就在想设置的目录或者文件夹下用git命令执行vagrant box add laravel/homestead,或者用cmd命令 ...

  5. cv2.VideoWriter()指定写入视频帧编码格式

    帧速率 fps 和 帧大小,通过VideoCapture类的get()函数得到. 编码参数:cv2.VideoWriter_fourcc('I','4','2','0')---未压缩的YUV颜色编码, ...

  6. c++学习(三)------static数据与成员函数

    疑惑: static类型成员是类的全局变量,所有类的实例都享有这个变量,或者说这个变量不属于任何一个类的实例. static类型变量可以为private,或public或其他(static数据可以被继 ...

  7. ASP.NET Core分布式项目-2.oauth密码模式identity server4实现

    源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...

  8. Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)

    大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...

  9. 奇妙的算法【9】YC每个小孩的糖果数,找公约数,最少硬币数

    1,每个小孩的糖果数量是多少 有p个小孩,c个糖果,刚开始第1个小孩发一个糖果,第2个小孩发两个糖果,第p个小孩发p个糖果,如果糖果没有发完,就接着[注意]第1个小孩发p+1个糖果.....第p个小孩 ...

  10. 再谈.NET委托(delegate、Func<>)

    为了演示委托,我们先来定义一个方法:public static bool IsTen(int i){    return i == 10 ? true : false;} 如果要用自定义委托,则需要声 ...