[洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
来源:互联网 发布:算法工程师年薪 编辑:程序博客网 时间:2024/04/30 20:55
题目背景
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)。
输出格式:只有一行,为一个整数,表示所需要的钱数。
第一眼以为是个DP 但是发现有后效性
有点像网络流的思想 我们得有一个源泉 那就是井 而且必须有一口井
所以我们把井当做一个点 把它与所有点连边 边权为在那个点打井的费用
然后各个点连引水边
跑一次最小生成树 就OK了
源代码
#include <bits/stdc++.h>using namespace std;int top, n, fa[305], fe[305], mp[305][305], head[305];struct Node{ int x, y, nxt, w; Node() { } Node( int x, int y, int w, int nxt ) : x(x), y(y), w(w), nxt(nxt) { } inline bool operator < ( const Node &a ) const { return w < a.w; }} e[150005];inline void Adde( int x, int y, int w ){ e[++top] = Node(x, y, w, head[x]), head[x] = top; e[++top] = Node(y, x, w, head[y]), head[y] = top;}int Find( int x ){ return x == fa[x] ? x : fa[x] = Find(fa[x]);}int main(){ cin >> n; for(int i = 1; i <= n; ++i) fa[i] = i, scanf( "%d", &fe[i] ); for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) scanf( "%d", &mp[i][j] ); for(int i = 1; i <= n; ++i) for(int j = i + 1; j <= n; ++j) Adde(i, j, mp[i][j]); for(int i = 1; i <= n; ++i) Adde(0, i, fe[i]); sort(e + 1, e + top + 1); long long ans = 0; for(int i = 1; i <= top; ++i) { int u = Find(e[i].x), v = Find(e[i].y); if(u != v) { fa[u] = v; ans += e[i].w; } } cout << ans << endl; return 0;}
- [洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
- 洛谷 P1550 [USACO08OCT]打井Watering Hole
- 新的开始( [USACO08OCT]打井Watering Hole)
- [USACO08OCT]Watering Hole
- [Usaco08Oct] Watering Hole
- [USACO08OCT]Watering Hole
- 【USACO08OCT】浇水洞Watering Hole
- 【学术篇】洛谷1550——打井Watering Hole
- Watering the Fields (最小生成树)
- soj3366: Watering Hole_最小生成树Prim
- Watering Hole
- 【BZOJ3479】[Usaco2014 Mar]Watering the Fields【最小生成树】
- bzoj 3479: [Usaco2014 Mar]Watering the Fields 最小生成树
- SOJ 3366 watering hole
- Watering Hole(题解)
- USACO oct. 09 Watering Hole
- 图论之Watering Hole
- 洛谷 P1550 浇水
- xml格式报文的拼装,和解析成实体类
- oracle nvl使用小技巧1
- 汇总
- 手机校验和邮箱校验
- Security-OAuth2 密码模式之服务器实现(工具IDEA+Maven+springboot)
- [洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
- Idea中Terminal命令不能执行git命令解决办法
- 大数据时代:Kafka 如何做到 1 秒发布百万条消息
- Assets 使用Pdf 矢量图
- 麦子金服:重视平台的安全性与合规性
- PullToRefresh+TabLayout+侧滑菜单栏+无限轮播小圆点+Fragment多条目+收藏网页数据到SQLite数据库
- 如何重载流操作符
- scratch编程第四讲:主动出击
- 我们用4行代码节省了100万 相见恨晚的PCDN