题意:

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. C#事件(Event): 发布符合 .NET Framework Guidelines 的事件

    本文翻译整理自:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-event ...

  2. Date类(java.util)和SimpleDateFormat类(java.text)

    在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当前时间,我们来看下 Date 类的使用: 使用 Date 类的默 ...

  3. python线性数据结构

    1.栈(Stack)(后进先出) 栈的实现: class Stack: def __init__(self): self.items = [] def isEmpty(self): return se ...

  4. SparkStreaming-Kafka集成

    SparkStreaming-Kafka集成 参考链接: Spark Streaming + Kafka Integration Guide 文章基本是官方的翻译, 最多再加入了一小部分自己的思考在内 ...

  5. Prometheus+Alertmanager+Grafana监控组件容器部署

    直接上部署配置文件 docker-compose.yml version: '3' networks: monitor: driver: bridge services: prometheus: im ...

  6. 如何编写Robot Framework测试用例1---(基本格式篇)

    引子 我们使用符合Robot Framework规范的一种表格语法来编写测试用例.用例一般会是下面这个样子 这样的表格存储到一个文件中,就是一组测试用例.RF支持多种格式,如HTML,TSV,纯文本等 ...

  7. Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name

    目前有发现的两种情况 第一种:是在继承jpa的时候检查实体类@id和@Entity引进的包是否是 import javax.persistence.Id imprt javax.persistence ...

  8. svn和 android adt的 eclipse插件更新地址

    下边这两个插件的更新地址是每次安装android开发环境时都能用到的,为了方便在这里记录一下. android adt: http://dl-ssl.google.com/android/eclips ...

  9. [UVA1494] Qin Shi Huang's National Road System

    题目 戳这里 题解 从今天起我要改邪归正,好好刷题准备联赛! 这是一道经典的最小生成树题目. 枚举每一条边作为道士要修的路,求出包含这条边的最小生成树. 先求出原图的最小生成树. 如果要删的边在最小生 ...

  10. 正斜杠(" / ")和反斜杠(" \ ")的区别

    反斜杠“\”是电脑出现了之后为了表示程序设计里的特殊含义才发明的专用标点.所以除了程序设计领域外,任何地方都不应该使用反斜杠. 如何区分正反斜杠 英语:"/" 英文是forward ...