洛谷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 ...
随机推荐
- Nginx 413 Request Entity Too Large
用户上传图片的时候,报错. 发现,原来是图片太大导致. 咦?后台配置图片支持5M啊? 哦!原来是Nginx配置问题. Nginx默认支持1M的POST数据! 修改Nginx配置! 修改nginx.co ...
- C++Primer 5th Chap9 Sequential Container
vector 可变大小数组,支持快速随机访问(在除了尾部之外部分插入删除元素很慢) deque 双端队列,支持快速随机访问(在头尾插入删除元素很快) list 双向链表,仅支持双向顺序访问(在任何位置 ...
- spring cloud微服务实践一
最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, sprin ...
- MySQL Sakila示例数据库
Table of Contents 1 Preface and Legal Notices 2 Introduction 3 History 4 Installation 5 Structure ...
- 面试经典算法:马拉松算法,最长回文子串Golang实现
求一个字符串中最长的回文子串. package main import "fmt" /* 马拉松算法,求最长回文子串,时间复杂度:线性 */ func main() { // 回文 ...
- (十五)Activitivi5之多用户任务分配
一.概念 我们在开发的时候,有一种情况是这样的, 我们有一个任务,可以让多个用户中的任何一个人办理即可,比如某个审批任务, 张三,李四,王五他们中的任何一人办理下都行,这时候,我们用到多用户任务分配. ...
- 北京大学1001ACM——高精度类型题总结
题目描述: ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 80517 Accepted: 190 ...
- vue引入警告:There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these
在写vue项目的时候 当我使用 : import dataSource from '../overseaProduct/house/dataSource'; 引入dataSource文件的时候:控制台 ...
- JFinal(1)JFinal helloworld
** java的极速开放框架:Final 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful MVC架构,设 ...
- 学习笔记-Rabin-Karp哈希
在数学一本通上看过这两人名字,现在又出现了... 思想: 用一个整数表示一个字符串 \(w_{str}\)=(\(a_0\) \(p^{n-1}\)+\(a_1\) \(p^{n-2}\)+...+\ ...