1601: [Usaco2008 Oct]灌水

Time Limit: 5 Sec  Memory Limit: 162 MB

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


这道题,一开始以为是贪心脑补了半天,竟然是最小生成树。我表示再不独立思考我就要跪了!最小生成树就很好想了,虚拟一个点连所有的水库,然后田地之间的边相互连,这样一遍最小生成树就可以求得最小答案了!

代码:

/*Author:WNJXYK*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
using namespace std; #define LL long long inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
inline void swap(LL &x,LL &y){LL tmp=x;x=y;y=tmp;}
inline int remin(int a,int b){if (a<b) return a;return b;}
inline int remax(int a,int b){if (a>b) return a;return b;}
inline LL remin(LL a,LL b){if (a<b) return a;return b;}
inline LL remax(LL a,LL b){if (a>b) return a;return b;} int Father[500];
inline void initFather(int n){
for (int i=0;i<=n;i++) Father[i]=i;
}
inline int getFather(int x){
return Father[x]=Father[x]==x?x:getFather(Father[x]);
}
inline void mergeFather(int x,int y){
int fx=getFather(x),fy=getFather(y);
if (fx<fy){
Father[fy]=Father[fx];
}else{
Father[fx]=Father[fy];
}
} struct Edge{
int u,v,value;
Edge(){}
Edge(int x,int y,int z){u=x;v=y;value=z;}
};
Edge e[50000];
int nume=0;
inline void addEdge(int x,int y,int cost){
e[++nume]=Edge(x,y,cost);
} inline bool cmp(Edge a,Edge b){
if (a.value<b.value) return true;
return false;
} int n;
int cnt;
int Ans;
int main(){
scanf("%d",&n);
initFather(n);
cnt=n+1;
Ans=0;
for (int i=1;i<=n;i++){
int tmp;
scanf("%d",&tmp);
addEdge(0,i,tmp);
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
int tmp;
scanf("%d",&tmp);
if (j>i) addEdge(i,j,tmp);
}
}
sort(e+1,e+nume+1,cmp);
for (int i=1;i<=nume;i++){
int x=e[i].u,y=e[i].v,c=e[i].value;
if (getFather(x)!=getFather(y)){
cnt--;
Ans+=c;
mergeFather(x,y);
}
if (cnt==1) break;
}
printf("%d\n",Ans);
return 0;
}

BZOJ 1601 [Usaco2008 Oct]灌水的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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. DropDownList为啥总是获取第一项的值???

    小菜: DropDownList控件绑定的数据,在获取数据时总是获取到第一项,很是郁闷,怎么回事,于是就各种想,都没有找到问题的原因. 请看下面的代码 前台代码: <asp:DropDownLi ...

  2. 【转】Linq实现DataTable行列转换

    出处:http://www.cnblogs.com/li-peng/ 转换前的table: 转换后的table: 代码里有详细的说明, 还有一些参数我都截图了下面有 using System;usin ...

  3. BestCoder Round #38

    1001 Four Inages Strategy 题意:给定空间的四个点,判断这四个点是否能形成正方形 思路:判断空间上4个点是否形成一个正方形方法有很多,这里给出一种方法,在p2,p3,p4中枚举 ...

  4. Maven POM配置释疑

    1.  对于有父子关系的Project, 父POM中依赖使用dependencies 和 dependencyManagement 的区别: dependencies: 即使子项目中不写该依赖项,仍然 ...

  5. [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析 ...

  6. poj 3243 Clever Y 高次方程

    1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...

  7. mapper分页排序指定字段查询模板

    StudentQuery: package Student; import java.util.ArrayList; import java.util.List; public class Stude ...

  8. 在GridView中实现全选反选的例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView控件.asp ...

  9. Jass 技能模型定义(转)

    Jass是什么?       先阐释一下什么是jass吧,百度:JASS(正确地说是JASS 2)是魔兽3的程序语言,用于控制游戏和地图的进行,也是魔兽游戏和地图的基础. 地图编辑器中摆放的单位(Un ...

  10. 贫血模型or领域模型

    参考: http://lifethinker.iteye.com/blog/283668 http://www.uml.org.cn/mxdx/200907132.asp http://www.itu ...