P5687 [CSP-S2019 江西] 网格图

题目描述

给定一个 n×m 的网格图,行从 1∼n 编号,列从 1∼m 编号,每个点可用它所在的行编号 r 与所在的列编号 c 表示为 (r,c)。

点 (i,j) 与 (i,j+1) 间连有一条权值为ai​ 的边,其中 1≤i≤n,1≤j

点 (i,j) 与 (i+1,j) 间连有一条权值为 bj​ 的边,其中 1≤i

请你求出这个网格图的最小生成树。

输入格式

第一行两个正整数 n,m 表示行数与列数。

第二行 n 个正整数表示 ai​。

第三行 m 个正整数表示 bj​。

输出格式

仅一行一个整数表示答案。

输入输出样例

输入 #1复制

3 3
2 4 3
1 3 2

输出 #1复制

16

题解:

很抱歉,博主最近很忙,随后会更新详细的题解

Ac code(It is so short,but difficult!):

#include
#include
int w[300005],h[300005],n,m,hi=2,wi=2,hide_h=1,hide_w=1,sum;
int main(){
	scanf("%d%d",&n,&m);
	for(register int i=1;i<=n;i++)scanf("%d",&h[i]);
	for(register int i=1;i<=m;i++)scanf("%d",&w[i]); 
	std::sort(h+1,h+n+1),std::sort(w+1,w+m+1);
	long long ans=1ll*h[1]*(m-1)+1ll*w[1]*(n-1);sum=n+m-2;
	while(sum!=n*m-1){
		if(h[hi]

End~~~

谢谢大家支持

你可能感兴趣的:(编程,信息学竞赛,竞赛题解库,算法,动态规划,c++,蓝桥杯,数据结构)