Watering Hole
2016-10-10 17:25
204 查看
题目背景
John的农场缺水了!!!题目描述
Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).
Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).
Determine the minimum amount Farmer John will have to pay to water all of his pastures.
翻译
农民John 决定将水引入到他的边长为n(1<=n<=300)的牧场。他准备通过挖若干井,并在各块田中修筑水道来连通各块田地以供水。在第i 号田中挖一口井需要花W_i(1<=W_i<=100,000)元。连接i 号田与j 号田需要P_ij (1 <= P_ij <= 100,000 , P_ji=P_ij)元。请求出农民John 需要为连通整个牧场的每一块田地所需要的钱数。
输入输出格式
输入格式:
第1 行为一个整数n。第2 到n+1 行每行一个整数,从上到下分别为W_1 到W_n。
第n+2 到2n+1 行为一个矩阵,表示需要的经费(P_ij)。
输出格式:
只有一行,为一个整数,表示所需要的钱数。输入输出样例
输入样例#1:
4 5 4 4 3 0 2 2 2 2 0 3 3 2 3 0 4 2 3 4 0
输出样例#1:
9
说明
John等着用水,你只有1s时间!!!用最少的花费把所有点都连起来,最小生成树……井的话就看做一个超级源点0号点,这就成了求 0 - n 这 n + 1 个点的最小生成树。
代码如下
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int size = 200010; int n; int num[size]; struct dc { int f,t,d; }l[size]; int tot = 1; void build(int f,int t,int d) { l[tot].f = f; l[tot].t = t; l[tot].d = d; tot ++; } int f[size]; int find(int x) { if(f[x] == x) return x; return f[x] = find(f[x]); } bool cmp(dc a,dc b) { return a.d < b.d; } int main() { scanf("%d",&n); for(int i = 1 ; i <= n ; i ++) scanf("%d",&num[i]) , f[i] = i; for(int i = 1 ; i <= n ; i ++) for(int j = 1 ; j <= n ; j ++) { int d; scanf("%d",&d); build(i,j,d); } for(int i = 1 ; i <= n ; i ++) build(0,i,num[i]); sort(l+1,l+tot,cmp); int ans = 0; for(int i = 1 ; i < tot ; i ++) { int ff = l[i].f , ft = l[i].t; if(find(ff) != find(ft)) { f[find(ff)] = find(ft); ans += l[i].d; } } printf("%d\n",ans); return 0; }
传送门:https://www.luogu.org/problem/show?pid=1550
相关文章推荐
- Android_自定义控件_ListView侧滑删除
- 使用记事本编写Java程序
- 非语言之争,Go vs Erlang
- Node.js操作redis示例及常用命令汇总
- openstack 国际化
- 五、Liunx 文件权限管理(二)变更文件所有者
- MyEclipse下Maven的安装配置
- 科大讯飞语音播报
- 百度sdk不显示地图
- 前缀式计算
- JS中用for循环解决重复定义的问题
- 合并表记录
- [More Effective C++]条款22有关返回值优化的验证结果
- ajax处理php返回json数据的实例代码
- 使用Bmob时遇到的Android依赖包冲突(Gson、Okhttp)
- 块级元素和内联元素(行内元素)
- iOS 手机rvictl抓包
- 开发环境搭建
- asp.net mvc短信接口调用——阿里大于API开发心得
- mysql 5.7.13 安装配置方法图文教程(win10) (转)