http://www.lydsy.com/JudgeOnline/problem.php?id=1601

很水的题,但是一开始我看成最短路了T_T

果断错。

我们想,要求连通,对,连通!连通的价值最小!当然是生成树!最小生成树!

边的还好做,但是这题有点,怎么办呢?

因为点在图中也起到连通作用,我们加个附加源,向每个点连边,价值为对应点权。

然后就ok了。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define rep(i, n) for(int i=0; i<(n); ++i)
  9. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  10. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  11. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  12. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  13. #define CC(i,a) memset(i,a,sizeof(i))
  14. #define read(a) a=getint()
  15. #define print(a) printf("%d", a)
  16. #define dbg(x) cout << #x << " = " << x << endl
  17. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  18. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  19. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  20. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  21.  
  22. const int N=500;
  23. int p[N], cnt, n;
  24. struct ED { int u, v, w; } e[N*N+N];
  25. const bool cmp(const ED &a, const ED &b) { return a.w<b.w; }
  26. void add(const int &u, const int &v, const int &w) {
  27. e[++cnt].u=u; e[cnt].v=v; e[cnt].w=w;
  28. }
  29. const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }
  30. int main() {
  31. read(n);
  32. for1(i, 1, n) add(0, i, getint());
  33. int fx, fy, ans=0;
  34. for1(i, 1, n) for1(j, 1, n) {
  35. read(fx);
  36. if(i!=j) add(i, j, fx);
  37. }
  38. sort(e+1, e+1+cnt, cmp);
  39. for1(i, 1, n) p[i]=i;
  40. for1(i, 1, cnt) {
  41. fx=ifind(e[i].u); fy=ifind(e[i].v);
  42. if(fx!=fy) {
  43. p[fx]=fy;
  44. ans+=e[i].w;
  45. }
  46. }
  47. print(ans);
  48. return 0;
  49. }

Description

Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。 建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。

Input

*第一行:一个数n *第二行到第n+1行:第i+1行含有一个数wi *第n+2行到第2n+1行:第n+1+i行有n个被空格分开的数,第j个数代表pij。

Output

*第一行:一个单独的数代表最小代价.

Sample Input

4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0

Sample Output

9

输出详解:

Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9

HINT

Source

【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)的更多相关文章

  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]灌水:最小生成树

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

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

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

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

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

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

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

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

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

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

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

  8. BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)

    题意: 300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价 思路: 先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑m ...

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

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

  10. 1601: [Usaco2008 Oct]灌水

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

随机推荐

  1. django 1.7 新特性 --- data migration

    官方文档:https://docs.djangoproject.com/en/dev/topics/migrations/ 1.7 之前大家可能会用south用于管理数据库的模型的同步.1.7之后dj ...

  2. ecshop的商品系统数据库整合

    -- 表的结构 `ecs_shop_config` '全站配置信息表'  商店设置   `id`   '全站配置信息自增id',`parent_id`  '父节点id,取值于该表id字段的值',`co ...

  3. 【VirtualBox】VirtualBox的桥接网络模式,为啥网络不稳定?

    网桥模式访问外网非常慢,经常卡死,ping时断时续 七搞八搞,反复重启了几次 TMD  就好了,也不知道什么情况,VirtualBox还是不太好使啊..... 网桥模式 设置 如下: 参考资料: ht ...

  4. Flatten Binary Tree to Linked List

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  5. Android Services重点记录

    今天阅读了google的官方文档 Services,对重点做下记录. 首先,Services默认运行在主线程中,所以一般情况下,要手动创建一个thread. 系统除了Services,还为我们提供了一 ...

  6. Android 阅读Tasks and Back Stack文章后的重点摘抄

    这篇文章是做android的必读篇目,要仔细阅读,原文连接http://developer.android.com/guide/components/tasks-and-back-stack.html ...

  7. (原创)Python文件与文件系统系列(2)——os模块对文件、文件系统操作的支持

    os模块的功能主要包括文件系统部分和进程管理部分,这里介绍其中与文件系统相关的部分. 当请求操作系统执行操作失败时,os模块抛出内置异常 exceptions.OSError 的实例,可以通过 os. ...

  8. MFC RadioButton

    添加一组RadioButton 多个radio button,IDC_RADIO1,IDC_RADIO2,IDC_RADIO3 ..将IDC_RADIO1的Group属性选择上,其他不要选Group属 ...

  9. Java中栈结构的自我实现

    package com.pinjia.shop.common.collection; /** * Created by wangwei on 2017/1/3. */ public class MyL ...

  10. 第六步:Lucene查询索引

    package cn.harmel.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache. ...