Watering Hole

来源:互联网 发布:非洲之傲豪华列车 知乎 编辑:程序博客网 时间:2024/05/01 00:51

题目背景

John的农场缺水了!!!

题目描述

Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.
Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).
Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).
Determine the minimum amount Farmer John will have to pay to water all of his pastures.

翻译

农民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 需要为连通整个牧场的每一块田地所需要的钱数。

输入输出格式

输入格式:

第1 行为一个整数n。
第2 到n+1 行每行一个整数,从上到下分别为W_1 到W_n。
第n+2 到2n+1 行为一个矩阵,表示需要的经费(P_ij)。

输出格式:

只有一行,为一个整数,表示所需要的钱数。

输入输出样例

输入样例#1:

454430 2 2 22 0 3 32 3 0 42 3 4 0

输出样例#1:

9

说明

John等着用水,你只有1s时间!!!
用最少的花费把所有点都连起来,最小生成树……井的话就看做一个超级源点0号点,这就成了求 0 - n 这 n + 1 个点的最小生成树。
代码如下

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int size = 200010;int n;int num[size];struct dc{    int f,t,d;}l[size];int tot = 1;void build(int f,int t,int d){    l[tot].f = f;    l[tot].t = t;    l[tot].d = d;    tot ++;}int f[size];int find(int x){    if(f[x] == x)        return x;    return f[x] = find(f[x]);}bool cmp(dc a,dc b){    return a.d < b.d;}int main(){    scanf("%d",&n);    for(int i = 1 ; i <= n ; i ++)        scanf("%d",&num[i]) , f[i] = i;    for(int i = 1 ; i <= n ; i ++)        for(int j = 1 ; j <= n ; j ++)        {            int d;            scanf("%d",&d);            build(i,j,d);        }    for(int i = 1 ; i <= n ; i ++)        build(0,i,num[i]);    sort(l+1,l+tot,cmp);    int ans = 0;    for(int i = 1 ; i < tot ; i ++)    {        int ff = l[i].f , ft = l[i].t;        if(find(ff) != find(ft))        {            f[find(ff)] = find(ft);            ans += l[i].d;        }    }    printf("%d\n",ans);    return 0;}

传送门:https://www.luogu.org/problem/show?pid=1550

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族手机有指纹和密码怎么办刷机 魅族手机指纹解锁密码忘了怎么办 魅蓝5s运存占用太多怎么办 魅蓝e2手机照片被删了怎么办 魅蓝e2不小心删除了照片怎么办 魅蓝3s返回键失灵怎么办 糖猫电话手表屏碎了怎么办 魅蓝手机没下安装包强制更新怎么办 老婆赌博输了30多万现在怎么办啊 红米nt2手机通话声音小怎么办? 微信退出后重新登录忘记密码怎么办 微退出后再登录忘记密码了怎么办 忘记微信密码又退出微信怎么办 无线网自家密码忘了也连不上怎么办 无线网密码忘了连接不上怎么办 魅蓝手机插口半夜坏了怎么办 魅族手机换屏后出现跳屏怎么办 小米手机微信小程序转发不出怎么办 苹果手机ad码和密码忘了怎么办 魅族手机摔掉无法开机怎么办 魅族音量+电源键直接开机了怎么办 魅蓝2数字锁机了怎么办 苹果5s蓝屏开不了机怎么办 红米pro更新开发版发热卡怎么办 5s手机音量增大键失灵怎么办 眼睛看手机久了模糊应怎么办 华为无线自拍杆连opp手机怎么办 苹果手机进水后自动跳电怎么办 魅族手机返回键不好使怎么办 手机壳是玻璃的摔坏了怎么办 魅族手机进水了开不了机怎么办 打发票机屏忘记了密码怎么办 魅族手机升级显示电量不足怎么办 魅蓝手机的图库没了怎么办 魅蓝5s屏幕碎了怎么办 魅族手机中间的home键失灵怎么办 魅族手机突然关机开不了机了怎么办 魅蓝3s卡顿怎么办视频 魅蓝3s手机太卡怎么办 红米4x外放破音怎么办 魅族手机刷机后内存变小了怎么办