题解 P1550 【[USACO08OCT]打井Watering Hole】
题面(翻译有点问题,最后一句话)
农民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 需要为使所有农场都与有水的农场相连或拥有水井所需要的钱数。
题意
有n个点,每个点之间都有边权,但是每个点也有点权。
要求算出每个点都连通的情况下(但是不要求每个点的点权都算上),最小价值。
题解
这题很容易想到最小生成树,但是又有好多人不敢下手去写最小生成树,因为这里的每个点还有点权。
我们可以转换一下思路,把每个点的点权当成一个指向自己的边权,然后构造一棵最小生成树就好了!
代码
#include<bits/stdc++.h>
using namespace std;
const int maxx = 1e3+10;
int n,cnt = 0,fa[maxx*maxx];
struct edge{
int u,v,w;
bool operator < (const edge &qwq)
{
return w < qwq.w;
}
}G[maxx*maxx];
inline void init()
{
cin >> n;
for(int i = 1;i <= n;++i)
{
int w;
cin >> w;
G[++cnt].u = 0;
G[cnt].v = i;
G[cnt].w = w;
}
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= n;++j)
{
int w;
cin >> w;
if(i > j)
{
G[++cnt].u = i;
G[cnt].v = j;
G[cnt].w = w;
}
}
}
for(int i = 1;i <= cnt;++i) fa[i] = i;
}
inline int fin(int x)
{
return fa[x] == x ? x : fa[x] = fin(fa[x]);
}
inline int kul()
{
sort(G+1,G+cnt+1);
int ans = 0,qwq = 0;
for(int i = 1;i <= cnt;++i)
{
int x = fin(G[i].u),y = fin(G[i].v);
if(x != y)
{
fa[x] = y;
++qwq;
ans += G[i].w;
}
if(qwq == n) break;
}
return ans;
}
int main(int argc, char const *argv[])
{
init();
cout << kul();
return 0;
}
题解 P1550 【[USACO08OCT]打井Watering Hole】的更多相关文章
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)
P1550 [USACO08OCT]打井Watering Hole 对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)
题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...
- luogu P1550 [USACO08OCT]打井Watering Hole
题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...
- P1550 [USACO08OCT]打井Watering Hole
题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...
- 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】
本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...
- Luogu P1550 打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
- [USACO08OCT]:打井Watering Hole(MST)
题意:有N个牧场,每个牧场修水井花费Wi,连接牧场花费Pij,问最小花费,使得每个牧场要么有水井,要么和有水井的牧场有通道. 思路:加一个格外的节点O,连接O表示修井,边权是修井的费用. 那么 ...
随机推荐
- 并发编程之 Java 三把锁
前言 今天我们继续学习并发.在之前我们学习了 JMM 的知识,知道了在并发编程中,为了保证线程的安全性,需要保证线程的原子性,可见性,有序性.其中,synchronized 高频出现,因为他既保证了原 ...
- C# ABP WebApi与Swagger UI的集成
本文是配置WebApi与Swagger UI,可以参照 http://www.cnblogs.com/farb/p/ABPSwaggerUIIntegration.html 1. 安装swagger ...
- golang中的接口实现(一)
golang中的接口实现 // 定义一个接口 type People interface { getAge() int // 定义抽象方法1 getName() string // 定义抽象方法2 } ...
- [日常] Go语言圣经-指针对象的方法-bit数组习题
练习6.1: 为bit数组实现下面这些方法 func (*IntSet) Len() int // return the number of elements func (*IntSet) Remov ...
- JavaScriptDay3
js对标签的操作 创建标签:document.createElement("tag"); 便签添加内容 : document.createElement("tag&quo ...
- Romantic(hdu2699+欧几里德)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Java - "JUC" ReentrantReadWriteLock
Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock ReadWriteLock 和 ReentrantReadWriteLock介绍 ReadWriteLo ...
- Java JDBC SqlServer
一.驱动 下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=11774 引用Jar包时注意区分Java版本: Sqljdbc4 ...
- ApplicationListener用法
ApplicationListener是spring提供的接口,作用是在web服务器启动时去加载某些程序. 用法: 1.实现ApplicationListener接口,并重写onApplication ...
- LOJ1070(SummerTrainingDay05-B 矩阵快速幂)
Algebraic Problem Given the value of a+b and ab you will have to find the value of an+bn. a and bnot ...