HDU:杭电 2018 复试真题汇总

1. 题目描述

瓜农王大爷去年种西瓜赚了不少钱。看到收入不错,今年他又重新开辟了 n 个西瓜地。

为了能给他的 n 个西瓜地顺利的浇上水,对于每个西瓜地他可以选择在本地打井,也可以修管道从另一个瓜地(这个瓜地可能打了井;也可能没打井,它的水也是从其它瓜地引来的)将水引过来。

当然打井和修管道的费用有差别。已知在第 i 个西瓜地打井需要耗费一定费用,在第 ij 个西瓜地之间修管道需要耗费一定费用。现在的问题是:王大爷要想使所有瓜地都被浇上水,至少需要花费多少钱(打井与修管道的费用和)?

由于瓜地较多,王大爷无法选择在哪些(个)瓜地打井,哪些西瓜地之间修管道。

请你编程帮王大爷做出决策求出最小费用。

输入格式

第 1 行,一个正整数 n,代表西瓜地的数量。

第 2 行,依次给出整数 w_1, w_2 \\dots w_n(每块西瓜地的打井费用),两个数之间有一个空格隔开。

紧接着是一个 n \\times n 的整数矩阵,矩阵的第 i 行第 j 列的数代表(两块西瓜地之间建立管道的费用),每行的两个数之间有一个空格隔开。

输出格式

一个正整数,所求的最小花费。

数据规模和约定

对于所有数据,1 \\le n \\le 300,打井费用 1 \\le w_i \\le 100000,管道费用及矩阵对角线元素(通常约定为0或无意义), 1 \\le p_{i,j} \\le 100000。(注:原图此处印刷模糊,此为根据常规题意推断的合理范围)

样例 1 样例 2 样例 3
输入 6 5 4 4 3 1 20 0 2 2 2 9 9... (矩阵首行) 6 10 4 5 3 1 20 0 2 2 2 9 8... (矩阵首行) 1 1 0
输出 19 18 1
cpp 复制代码
#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<algorithm>

using namespace std;
int melon[305];
int vis[305];
int parent[305];

typedef struct {
	int begin;
	int end;
	int value;
}edge;

bool cmp(edge A,edge B) {
	return A.value<B.value;
}

int findroot(int x){
	if(parent[x]==x) return x;
	return parent[x]=findroot(parent[x]);
}

int main() {
	int n;
	while (cin >> n) {
		vector<edge>s1;
		edge temp;
		
		for(int i=0;i<n;i++) {
			cin>>melon[i];
			temp.begin=n;
			temp.end=i;
			temp.value=melon[i];
			s1.push_back(temp);	
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				temp.begin=i;
				temp.end=j;
				cin>>temp.value;
				s1.push_back(temp);
			}
		}
		
		sort(s1.begin(),s1.end(),cmp);
		
		int sum=0;
		int count=0;
		
		for (int i = 0; i <= n; i++) {
			parent[i] = i; 
		}
		
		for(int i=0;i<s1.size();i++) {
			temp=s1[i];
			if(findroot(temp.begin)!=findroot(temp.end)) {
				parent[findroot(temp.end)]=findroot(temp.begin);
				sum+=temp.value;
				count++;
				vis[temp.begin]=1;
				vis[temp.end]=1;
			}
			if(count==n)
				break;
		}
		cout<<sum<<endl;
	}
	return 0;
}
相关推荐
会编程的土豆6 小时前
洛谷题单入门1 顺序结构
数据结构·算法·golang
生信碱移6 小时前
PACells:这个方法可以鉴定疾病/预后相关的重要细胞亚群,作者提供的代码流程可以学习起来了,甚至兼容转录组与 ATAC 两种数据类型!
人工智能·学习·算法·机器学习·数据挖掘·数据分析·r语言
智者知已应修善业7 小时前
【51单片机中的打飞机设计】2023-8-25
c++·经验分享·笔记·算法·51单片机
智者知已应修善业9 小时前
【51单片机按键调节占空比3位数码管显示】2023-8-24
c++·经验分享·笔记·算法·51单片机
.5489 小时前
## Sorting(排序算法)
python·算法·排序算法
wuweijianlove10 小时前
算法的平均复杂度建模与性能回归分析的技术7
算法·数据挖掘·回归
子琦啊10 小时前
【算法复习】字符串 | 两个底层直觉,吃透高频题
linux·运维·算法
code_pgf11 小时前
Octo 算法详解-开源通用机器人策略模型技术报告
算法·机器人·开源
嘻嘻哈哈樱桃11 小时前
牛客经典101题题解集--动态规划
java·数据结构·python·算法·职场和发展·动态规划
脱氧核糖核酸__12 小时前
LeetCode热题100——234.回文链表(两种解法)
c++·算法·leetcode·链表