洛谷 P1550 浇水
来源:互联网 发布:ubuntu 进入目录 编辑:程序博客网 时间:2024/04/29 23:46
洛谷 1550 浇水
题目背景
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.
POINTS: 400
农民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时间!!!
思路:看到题面很容易想到最小生成树,但这题难在有一个水井系统。
考虑到无论如何最少也需要打一口井,所以我们可以将水井看做一个点,连接田地和水
井的费用即打井的费用,之后就可以求最小生成树了。
题解:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct cc{ int from,to,cost;}es[100000];int val[505],f[505];int tot=0;void build(int ff,int tt,int pp){ tot++; es[tot].from=ff; es[tot].to=tt; es[tot].cost=pp;}int cmp(cc a,cc b){ return a.cost<b.cost;}int find(int w){ if(w!=f[w]) { f[w]=find(f[w]); } return f[w];}int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&val[i]); } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int fee; scanf("%d",&fee); build(i,j,fee); } } for(int i=1;i<=n;i++) { build(0,i,val[i]); } for(int i=1;i<=n;i++) { f[i]=i; } sort(es+1,es+n*n+n+1,cmp); int ans=0; for(int i=1;i<=n*n+n+1;i++) { if(find(es[i].from)!=find(es[i].to)) { f[find(es[i].from)]=find(es[i].to); ans+=es[i].cost; } } printf("%d",ans); return 0;}
- 洛谷 P1550 浇水
- 洛谷 P1550 [USACO08OCT]打井Watering Hole
- [洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
- 猴子浇水的故事
- 【浇水问题】解题报告
- 【单调栈】浇水
- 【数学题】浇水解题报告
- wpf 浇水灌溉小游戏
- 浇水问题c程序
- 【USACO08OCT】浇水洞Watering Hole
- uva10382 - Watering Grass(给草浇水)
- 兰花施肥、浇水的关键时间
- HTML5模拟给树浇水长大
- 爱情在悄悄开出花蕾的时候需要浇水培养
- 【效果】html5给小树浇水,然后树长大的效果
- Arduino 与 土壤湿度传感器 测量土壤湿度自动浇水提醒
- 2010山东省信息学夏令营模拟赛Test 5.浇水 题解
- 【案例分析-初探】美国去年卖了多少花园浇水用软管?
- 10、桥接模式(Bridge)
- 常用RGB颜色表
- [HDU]1394 Minimum Inversion Number (树状数组--单点更新,区间查询 求逆序数)
- 1060 搞笑世界杯
- js 生成表格 添加,删除行、列。行,列数据交换
- 洛谷 P1550 浇水
- 2016微博用户研究:新欢、旧爱、核心价值与迫切之疾
- ireport 中Image属性使用 图片显示 打印图片
- 97. Interleaving String
- 百度地图API:在RichMarker上使用InfoBox
- 百度地图API:在RichMarker上使用InfoBox
- 第七周 oj训练6
- vc6 使用winpcap时的环境配置
- Redis 菜鸟笔记(三)集群原理、搭建