对于自己建水库的情况,新建一个虚拟结点,和其他点的边权即为自建水库的费用
这样问题就转化为一个裸最小生成树问题了。
这里用堆优化prim解决。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#define re register
using namespace std;
void read(int &x){
char c=getchar();x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
}
struct data{
int d,u;
data(){}
data(int A,int B):
d(A),u(B)
{}
bool operator < (const data &tmp) const{return d>tmp.d;}
};priority_queue <data> h;
#define N 305
int n,a[N][N],d[N],ans; bool vis[N];
int main(){
read(n);++n; int k=;
for(re int i=;i<n;++i)
read(a[n][i]),a[i][n]=a[n][i];//新建边
for(re int i=;i<n;++i)
for(re int j=;j<n;++j)
read(a[i][j]);
memset(d,,sizeof(d));
h.push(data(d[]=,));
while(!h.empty()&&k<n){
data x=h.top(); h.pop();
if(vis[x.u]) continue;
vis[x.u]=; ++k; ans+=x.d;
for(re int i=;i<=n;++i)
if(a[x.u][i]){
if(d[i]>a[x.u][i]){
d[i]=a[x.u][i];
h.push(data(a[x.u][i],i));
}
}
}printf("%d",ans);
return ;
}

bzoj1601 / P1550 [USACO08OCT]打井Watering Hole(堆优化prim)的更多相关文章

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

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

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

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

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

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

  4. luogu P1550 [USACO08OCT]打井Watering Hole

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

  5. P1550 [USACO08OCT]打井Watering Hole

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

  6. 洛谷 题解 P1550 【[USACO08OCT]打井Watering Hole】

    本题看似很难,实际上思路非常简单--如果你想通了. 首先有一个问题:图中有几个点?大部分的人会回答\(n\)个点.错了,有\(n+1\)个. 多出来的那个点在哪?关键在于你要理解每一个决策的意义.实际 ...

  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. 【学术篇】洛谷1550——打井Watering Hole

    题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...

随机推荐

  1. 《C++ Primer Plus》第7章 函数——C++的编程模块 学习笔记

    函数是C++的编程模块.要使用函数,必须提供定义和原型,并调用该函数.函数定义是实现函数功能的代码:函数原型描述了函数的接口:传递给函数的值的书目和种类以及函数的返回类型.函数调用使得程序将参数传递给 ...

  2. AndroidのUI布局之layout weight

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. Git介绍和基本原理

    官方文档:http://git-scm.com/doc 1.1 起步 - 关于版本控制 本章关于开始学习 Git. 我们从介绍有关版本控制工具的一些背景知识开始,然后讲解如何在你的系统运行 Git,最 ...

  4. Android Runtime.getRuntime().exec

    try { // Executes the command. Process process = Runtime.getRuntime().exec(cmd); // NOTE: You can wr ...

  5. 以用户名注册来分析三种Action获取数据的方式

    1.注入属性 直接注入属性: public String userName; public String getUserName() { return userName; } public void ...

  6. 域渗透学习预备知识-IPC$的入侵防御

    一.什么是IPC$ 以下段落引文自:http://www.xfocus.net/articles/200303/493.html IPC$(Internet Process Connection)是共 ...

  7. 【BZOJ1453】[Wc]Dface双面棋盘 线段树+并查集

    [BZOJ1453][Wc]Dface双面棋盘 Description Input Output Sample Input Sample Output HINT 题解:话说看到题的第一反应其实是LCT ...

  8. http://blog.csdn.net/ym19860303/article/details/8462450

    http://www.himigame.com/iphone-cocos2d/444.html 很多游戏中,便于开发就会直接采用Cocos2d提供的字体库来使用,但是因为提供的种类很多,那么Himi利 ...

  9. ionic开发过程中遇到的一些坑!

    总结一些:在使用 ionic 开发过程中所遇到的问题. 问题一:Cannot find module '@ionic/app-scripts' 描述:使用 ionic start 项目的时候,项目安装 ...

  10. git下载和上传项目

    首先是git的下载和安装: https://www.cnblogs.com/chenxqNo01/p/6372933.html git的简单使用: 从码云 clone 项目: git clone ht ...