洛谷1546 最短网络Agri-Net【最小生成树】【prim】
【内含最小生成树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】的更多相关文章
- 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)
洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...
- 洛谷 P1546 最短网络 Agri-Net(最小生成树)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1546 首先不难看出这道题的思想是用了最小生成树,但是这道题有难点: 1.读题读不明白 2.不会读入 ...
- 洛谷 P1546 最短网络 Agri-Net
题目链接 https://www.luogu.org/problemnew/show/P1546 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当 ...
- 洛谷P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...
- 洛谷——P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...
- 洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树
题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...
- 洛谷 P1546 最短网络 Agri-Net(最小生成树)
题目链接 https://www.luogu.org/problemnew/show/P1546 说过了不复制内容了 显然是个最小生成树. 解题思路 prim算法 Kruskal算法 prim算法很直 ...
- 洛谷 P1546 最短网络 Agri-Net x
题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其 ...
- 洛谷P1546 最短网络 Agri-Net(Prim堆优化)
#include<bits/stdc++.h> using namespace std; ; const int INF=0x3f3f3f3f; inline void read(int ...
随机推荐
- java输入输出 -- I/O模型简述
一.简介 本文向大家介绍五种I/O模型.分别是阻塞I/O.非阻塞I/O.I/O复用.信号驱动式I/O.异步I/O等.内容参考<UNIX网络编程>,大家想进深入学习网络编程,建议去读读这本书 ...
- Linux/ubuntu 心得
基本命令 有n个软件未被升级(有强迫症的,不爽的 apt-get dist-upgrade 更改主机名字 git 不要免密输入的话,可在当前工作目录执行 git config credential.h ...
- Thinking In Java 4th Chap6 访问权限控制
引入一个包及其所包含的方法:import java.util.ArrayList;(引入java.util包,并引入了包中的ArrayList类) import java.util.*;(引入了jav ...
- docker 的常用命令汇总
- Winscp隧道实现-跳板机/跨机连接
隧道用的是公网ip,登陆用的是私网ip 一张图应该就能看懂,后续用到新的功能继续编辑
- MongoDB添加删除节点
副本集添加删除节点 sharding添加删除节点 先将节点设置为hidden,再remove
- Linux虚拟机常用命令
参考原文链接:(https://blog.csdn.net/fanyun_01/article/details/51083585) 一.Linux虚拟机常用命令 # virsh list //查看已打 ...
- 正则表达式的运行原理详解(NFA,多分支原理)
原文:https://blog.csdn.net/yx0628/article/details/82722166
- 此项目与Visual Studio的当前版本不兼容的报错
问题再现:程序是用visual studio 2013开发的,放在本地运行报此项目与Visual Studio的当前版本不兼容.本地是visual studio 2010. 解决办法: <1&g ...
- IntelliJ IDEA 之 配置JDK 的 4种方式
一.新建项目前配置JDK 打开IDEA集成开发环境工具,点击:File--Project Structure,如下图 在打开的页面中,选择SDKs属性,并点击中间的加号+,选择JDK,如下图 在打开的 ...