[USACO08OCT]Watering Hole
来源:互联网 发布:mac版本qq游戏大厅 编辑:程序博客网 时间:2024/04/30 20:46
传送门
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
输入格式:
第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
获得最小生成树的新姿势×1
设定一个事实上不存在的点,将所有的点向其连边,边权为在该土地上打井所需的费用,正常做最小生成树,AC~ 我才不会告诉你我把并查集写错了呢
CODE:
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;struct aa{ int from,to,dis;}a[100001];int w[301];int c[301][301];int f[305];int n,num,k,x,y,ans;inline void read(int &n){ n=0;char c;bool b=0; while((c=getchar())==' '||c=='\n'||c=='\r'); if(c=='-') b=1; else n=c-48; while(isdigit(c=getchar())) n=n*10+c-48; if(b) n*=-1;}inline void add(int from,int to,int dis){ a[++num].from=from,a[num].to=to,a[num].dis=dis;}int find(int n){ if(f[n]!=n) f[n]=find(f[n]); return f[n];}bool cmp(aa a,aa b){ return a.dis<b.dis;}int main(){ read(n); for(int i=1;i<=n;i++) read(w[i]); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) read(c[i][j]); for(int i=1;i<=n;i++) f[i]=i; for(int i=1;i<=n;i++) for(int j=1;j<i;j++) add(i,j,c[i][j]); for(int i=1;i<=n;i++) add(0,i,w[i]); sort(a+1,a+num+1,cmp); for(int i=1;i<=num;i++) { x=find(a[i].from),y=find(a[i].to); if(x!=y) { f[y]=x; ans+=a[i].dis; k++; } if(k==n) break; } printf("%d",ans); return 0;}
- [USACO08OCT]Watering Hole
- [Usaco08Oct] Watering Hole
- [USACO08OCT]Watering Hole
- 【USACO08OCT】浇水洞Watering Hole
- 洛谷 P1550 [USACO08OCT]打井Watering Hole
- [洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
- 新的开始( [USACO08OCT]打井Watering Hole)
- Watering Hole
- SOJ 3366 watering hole
- Watering Hole(题解)
- USACO oct. 09 Watering Hole
- 图论之Watering Hole
- [Usaco2008 Oct]Watering Hole 挖水井
- USACO08OCT
- USACO - Oct08 Gold Watering Hole 挖井 Kruskal演算法+点权转边权
- 【学术篇】洛谷1550——打井Watering Hole
- Coming to a watering hole near you: OLPC's mesh networking
- Hole
- 外挂学习之路(3)--- 内存遍历工具
- 挑战传统微积分的第一人
- 面向对象(1)
- eclipse安装activiti designer插件
- [盘点]现今热门的h5网游
- [USACO08OCT]Watering Hole
- 普通程序员如何转向AI方向
- IOS应用内跳转到系统设置
- 四个月的蜕变之数据库的常用操作命令
- 如何在win10(64位系统)上安装apache服务器
- javascript的知识点
- iOS常用验证
- java中日期格式的加减,Timestamp和util.Date格式互转
- hdfs常见故障