本题看似很难,实际上思路非常简单——如果你想通了。

首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点。错了,有\(n+1\)个。

多出来的那个点在哪?关键在于你要理解每一个决策的意义。实际上,多出来的那个点是地下的天然矿泉水。当我们打井时,我们实际上是在往地下连边。理解了这一点,代码就没有任何难度了。

构图时,我们只需多加一个点,对于每个点\(i\),我们连边\(i→n+1\),边权为\(w_i\)。然后直接跑最小生成树就没了。就没了。(转载from here)

#include<bits/stdc++.h>
using namespace std;
const int MAXN=300+10;
const int MAXM=1e5;
int n,m;
int fa[MAXN];
struct Node
{
int u,v,w;
bool operator < (const Node &x) const
{
return x.w>w;
}
}edge[MAXM];
inline int read()
{
int tot=0;
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9')
{
tot=tot*10+c-'0';
c=getchar();
}
return tot;
}
inline int find(int k)
{
if(fa[k]==k)return k;
else return fa[k]=find(fa[k]);
}
inline int kruskal()
{
int tot=0,cnt=0;
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
int fx=find(edge[i].u),fy=find(edge[i].v);
if(fx!=fy)
{
fa[fx]=fy;
tot++;
cnt+=edge[i].w;
}
if(tot==n-1)return cnt;
}
}
int main()
{
n=read();
int x;
for(int i=1;i<=n;i++)
{
x=read();
edge[++m].u=i;
edge[m].v=n+1;
edge[m].w=x;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
x=read();
if(i<j)
{
edge[++m].u=i;
edge[m].v=j;
edge[m].w=x;
}
}
}
n++;
sort(edge+1,edge+1+m);
printf("%d\n",kruskal());
return 0;
}

洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】的更多相关文章

  1. 洛谷P1550 [USACO08OCT]打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

  2. Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole

    题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...

  3. bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)

    P1550 [USACO08OCT]打井Watering Hole   对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用 这样问题就转化为一个裸最小生成树问题了. 这里用堆优化 ...

  4. 题解——洛谷P1550 [USACO08OCT]打井Watering Hole(最小生成树,建图)

    题面 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pas ...

  5. luogu P1550 [USACO08OCT]打井Watering Hole

    题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...

  6. P1550 [USACO08OCT]打井Watering Hole

    题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conven ...

  7. 题解 P1550 【[USACO08OCT]打井Watering Hole】

    题面(翻译有点问题,最后一句话) 农民John 决定将水引入到他的n(1<=n<=300)个牧场.他准备通过挖若 干井,并在各块田中修筑水道来连通各块田地以供水.在第i 号田中挖一口井需要 ...

  8. Luogu P1550 打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

  9. 洛谷 题解 UVA572 【油田 Oil Deposits】

    这是我在洛谷上的第一篇题解!!!!!!!! 这个其实很简单的 我是一只卡在了结束条件这里所以一直听取WA声一片,详细解释代码里见 #include<iostream> #include&l ...

随机推荐

  1. go get 无反应方法 Win/Linux 命令行、终端和 Git 代理设置

    go get -u -v 无反应方法 CMD要用管理员权限运行,否则设置无效 netsh winhttp set proxy proxy-server="https=127.0.0.1:10 ...

  2. ansys meshing当中共享节点的操作

    原视频下载地址: https://yunpan.cn/cYKuN9mIHMUxa  访问密码 5858

  3. Python安装第三方库常用方法

    在学习Python过程中,经常要用到很多第三方库,面对各种不同情况,Python为我们提供了多种安装方法: 一.pip安装: pip安装相信大家都不陌生了,在安装第三方库中,pip是最常使用的一种方法 ...

  4. Oracle导出/导入数据库的三种模式

    导出 模式一:全量导出(慎用) exp 用户名/密码@数据库实例 owner=用户名 file=文件存储路径 log=日志存储路径 full=y 栗子:exp Mark/123456@151.2.*. ...

  5. HearthBuddy的class276以及class247

    使用de4dot-cex反编译原版的hearthbuddy得到的 链接: https://pan.baidu.com/s/1hT79LpIjbyvODsjnkSe_5A 提取码: iemx class ...

  6. Unity制作王者荣耀商业级手游

    <王者荣耀>这种现象级手机游戏是如何制作出来的呢?本文以<王者荣耀>MOBO类型的多人在线战术竞技游戏为入口,覆盖Unity游戏制作开发前端与Node.js服务器端的开发必备知 ...

  7. TP5 分页数据加锚点

      TP5 分页数据加锚点跳转到相应位置   有这样一个需求,就是加载评论后,点下一页的时候回到相应的位置.  $comment = Db('comment')->order('addtime' ...

  8. 001 安装mysql

    在安装docker之后,安装一个mysql的容器试试手.可以参考违章: URL: https://www.cnblogs.com/limingxie/p/8655457.html

  9. CRM 公海 回收规则 AI

    7.3.2 客户和公海管理 · 纷享销客产品手册 https://www.fxiaoke.com/mob/guide/crmdoc/src/7-3-2%E5%AE%A2%E6%88%B7%E5%92% ...

  10. Vscode 调试 Flutter 项目

    1.Vscode 中打开 flutter 项目进行开发 2.运行 Flutter 项目 flutter run r 键:点击后热加载,也就算是重新加载吧. p 键:显示网格,这个可以很好的掌握布局情况 ...