题意:

300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价

思路:

先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑mst,这样就能保证所有坑联通,并且至少有一个坑有水

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
//#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int p[][];
int w[];
int n;
int f[maxn];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
struct node{
int x, y;
int w;
}edge[maxn];
bool cmp(node a, node b){
return a.w<b.w;
}
int tot;
int add(int x, int y, int w){
edge[++tot].x=x;
edge[tot].y=y;
edge[tot].w=w;
}
int main(){
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", &w[i]);
add(,i,w[i]);
}
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
scanf("%d", &p[i][j]);
add(i,j,p[i][j]);
}
}
for(int i = ; i <= n; i++)f[i]=i;
sort(edge+, edge++tot,cmp);
int cnt = ;
int ans = ;
for(int i = ; i <= tot; i++){
int x = edge[i].x;
int y = edge[i].y;
int w = edge[i].w;
int t1 = find(x);
int t2 = find(y);
if(t1 != t2){
f[t1] = t2;
ans+=w;
cnt++;
}
if(cnt==n)break;
}
printf("%d",ans);
return ;
} /*
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
*/

BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)的更多相关文章

  1. BZOJ 1601 [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...

  2. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...

  3. BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...

  4. BZOJ——1601: [Usaco2008 Oct]灌水

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit:  ...

  5. BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点

    Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...

  6. BZOJ 1601: [Usaco2008 Oct]灌水( MST )

    MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...

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

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

  8. bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】

    挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...

  9. 1601: [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1342  Solved: 881 [Submit][S ...

随机推荐

  1. linux下文件解压缩中文乱码问题的解决

    将带中文文件名的压缩文件上传到服务器,使用unzip解压后,文件名乱码: 临时解决方法: 通过unzip行命令解压,指定字符集unzip -O CP936 xxx.zip (用GBK, GB18030 ...

  2. TensorFlow——卷积神经网络的相关函数

    在TensorFlow中,使用tr.nn.conv2d来实现卷积操作,使用tf.nn.max_pool进行最大池化操作.通过闯传入不同的参数,来实现各种不同类型的卷积与池化操作. 卷积函数tf.nn. ...

  3. Spark集群-Standalone 模式

    Spark 集群相关 table td{ width: 15% } 来源于官方, 可以理解为是官方译文, 外加一点自己的理解. 版本是2.4.4 本篇文章涉及到: 集群概述 master, worke ...

  4. linux入门系列7--管道符、重定向、环境变量

    前面文章我们学习了linux基础命令,如果将不同命令组合使用则可以成倍提高工作效率.本文将学习重定向.管道符.通配符.转义符.以及重要的环境变量相关知识,为后面的shell编程打下基础. 一.IO重定 ...

  5. poj 2689 区间素数筛

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  6. 测试必备之Java知识(二)—— Java高级的东西

    Java高级 类加载过程 加载(创建class对象) -> 连接(验证-准备-解析) -> 类初始化 类加载器类别 根类加载器:加载java核心类 扩展类加载器:加载JRE目录中的jar包 ...

  7. python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接

    最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...

  8. msi通过powershell安装、卸载

    function install_msi($url) { $telemetry = @{ DisplayName = "Telemetry Service"; filename = ...

  9. Redis Sentinel 高可用机制

    内容目录: Sentinel 如何工作的? 核心配置项 怎么选出新 master 的? Sentinel 有多个,具体谁来执行故障转移? Sentinel 是怎么发现 slave 和其他 sentin ...

  10. 编写python程序读入1到100之间的整数,然后计算每个数出现的次数,输入0表示结束输人,输入数据不包括0。如果数出现的大现如果大于1,输出时使用复数times

    #-*- coding:UTF-8 -*- #环境:python3 print("Enter the numbers between 1 and 100:") enterList= ...