Wek6 Minimum spanning tree

2023-10-22 21:51
文章标签 tree minimum spanning wek6

本文主要是介绍Wek6 Minimum spanning tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题一描述:

东东在老家农村无聊,想种田。农田有 n 块,编号从 1~n。种田要灌氵
众所周知东东是一个魔法师,他可以消耗一定的 MP 在一块田上施展魔法,使得黄河之水天上来。他也可以消耗一定的 MP 在两块田的渠上建立传送门,使得这块田引用那块有水的田的水。 (1<=n<=3e2)
黄河之水天上来的消耗是 Wi,i 是农田编号 (1<=Wi<=1e5)
建立传送门的消耗是 Pij,i、j 是农田编号 (1<= Pij <=1e5, Pij = Pji, Pii =0)
东东为所有的田灌溉的最小消耗。

Input:
第1行:一个数n
第2行到第n+1行:数wi
第n+2行到第2n+1行:矩阵即pij矩阵

Output:
东东最小消耗的MP值。

Sample Input:
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0

Sample Output:
9

问题二描述:

在这里插入图片描述

Sample Input :
4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2

Sample Output:
4

解题思路:

题一的目的是让所有田都有水灌溉,且消耗最小,建立一个超级源点,题目等价于对这n+1个点建立最小生成树。
题二欲使树的最大边最小,欲使瓶颈生成树的最大边最小,该边等于最小生成树的边,故等价于求最小生成树的边。
最小生成树的思想:若有n个点,将边升序排序,从小到大依次取n-1条不会产生环的边,这n-1条边就构成了最终的最小生成树,可以利用并查集求最小生成树。

实验一代码:

#include<iostream>
#include<algorithm>
using namespace std;
int root[310];			
int rnk[310]; 			
struct Edge				
{int u;			int v;int w;bool operator<(const Edge& e) const	{return w<e.w;			}
};			
Edge edge[50000];	
int cnt;			 
void unit(int n)		
{int w;Edge e;for(int i=1;i<=n;i++)		{cin>>w;edge[cnt].u=0;edge[cnt].v=i;edge[cnt].w=w;cnt++;}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin>>w;				if(i>j){edge[cnt].u=i;edge[cnt].v=j;edge[cnt].w=w;cnt++;}}} sort(edge,edge+cnt);			for(int i=0;i<=n;i++)root[i]=i;		
}
int find(int a)		
{if(root[a]==a)		return a;elsereturn find(root[a]);	
}bool unite(int a,int b,int w)			
{int rootA=find(a);		int rootB=find(b);		if(rootA==rootB)return false;		else{root[rootB]=rootA;					rnk[rootA]+=(rnk[rootB]+w);				return true;} 
} 
bool min_spanningtree(int n)		
{int e=0;for(int i=0;i<cnt;i++){if( unite(edge[i].u,edge[i].v,edge[i].w) )		{e++;  if(e==n)		return true;}}
}int main(void)
{int n;cin>>n;unit(n);min_spanningtree(n);cout<<rnk[ find(0) ]<<endl;return 0;} 

实验二代码:

#include<iostream>
#include<algorithm> 
using namespace std;
int n,m,r;
struct Edge
{int u;int v;int w;bool operator<(const Edge& e) const{return w<e.w;} 
};
Edge edge[100050];
int ecnt;
int root[50050];
int rnk[50050];
int find(int a)
{if(root[a]==a)		 return a;return find(root[a]);
}
bool unite(int a,int b,int w)
{int classA=find(a);int classB=find(b);if(classA==classB)		return false;if(classA<classB)			swap(classA,classB);root[classB]=classA;		rnk[classA]+=(rnk[classB]+w);		return true;
} 
void initial()
{for(int i=0;i<=n;i++)root[i]=i;			
}
int spanning_Tree()
{sort(edge,edge+ecnt);		initial();			int e=0;				for(int i=0;i<ecnt;i++){if( unite(edge[i].u,edge[i].v,edge[i].w) )		{e++;  if(e==n-1)					return edge[i].w;		 }}
} 
int main(void)
{ios::sync_with_stdio(0);cin>>n>>m>>r;int u,v,w;for(int i=0;i<m;i++){cin>>u>>v>>w;edge[ecnt].u=u;edge[ecnt].v=v;edge[ecnt].w=w;ecnt++;}cout<<spanning_Tree()<<endl;return 0;
}

这篇关于Wek6 Minimum spanning tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/263804

相关文章

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

Sorry!Hbase的LSM Tree就是可以为所欲为!

我们先抛出一个问题: LSM树是HBase里使用的非常有创意的一种数据结构。在有代表性的关系型数据库如MySQL、SQL Server、Oracle中,数据存储与索引的基本结构就是我们耳熟能详的B树和B+树。而在一些主流的NoSQL数据库如HBase、Cassandra、LevelDB、RocksDB中,则是使用日志结构合并树(Log-structured Merge Tree,LSM Tr

【spring】does not have member field ‘com.sun.tools.javac.tree.JCTree qualid

spring-in-action-6-samples 的JDK版本 最小是11,我使用 了22: jdk21 jdk22 都与lombok 不兼容,必须使用兼容版本, 否则报错: thingsboard 的大神解释了: java: java.lang.NoSuchFieldError: Class com

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

[LeetCode] 863. All Nodes Distance K in Binary Tree

题:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/ 题目大意 求给树中,距给定 结点 指定长度的 所有结点的val 思路 tree -> graph 、 bfs 先遍历树,并用map记录每个结点的父结点 ,将树变为图,然后 bfs。 /*** Definition for a binary tree

js实现树级递归,通过js生成tree树形菜单(递归算法)

1、效果图 需求:首先这是一个数据集—js的类型,我们需要把生成一个tree形式的对象 : var data = [{ id: 1, name: "办公管理", pid: 0 },{ id: 2, name: "请假申请", pid: 1 },{ id: 3, name: "出差申请", pid: 1 },{ id: 4, name: "请假记录", pid: 2 },{ id:

【unity实战】利用Root Motion+Blend Tree+Input System+Cinemachine制作一个简单的角色控制器

文章目录 前言动画设置Blend Tree配置角色添加刚体和碰撞体代码控制人物移动那么我们接下来调整一下相机的视角效果参考完结 前言 Input System知识参考: 【推荐100个unity插件之18】Unity 新版输入系统Input System的使用,看这篇就够了 Cinemachine虚拟相机知识参考: 【推荐100个unity插件之10】Unity最全的最详细的C

树链剖分 + 后缀数组 - E. Misha and LCP on Tree

E. Misha and LCP on Tree  Problem's Link   Mean:  给出一棵树,每个结点上有一个字母。每个询问给出两个路径,问这两个路径的串的最长公共前缀。 analyse: 做法:树链剖分+后缀数组. 记录每条链的串,正反都需要标记,组成一个长串. 然后记录每条链对应的串在大串中的位置,对大串求后缀数组,最后询问就是在一些链