【BZOJ1601】【MST】灌水 题解
来源:互联网 发布:matlab 数组幅值 编辑:程序博客网 时间:2024/06/04 19:27
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
建一个水源的点,将初始点连进去跑一边最小生成树,完。
#include <cstdio>#include <algorithm>#define digit (ch < '0' || ch > '9')using namespace std;template <class T> inline void read(T &x) { int flag = 1; x = 0; register char ch = getchar(); while( digit) { if(ch == '-') flag = -1; ch = getchar(); } while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); } x *= flag;}const int maxn = 1105;int n,w,tot,ans;int fa[maxn];struct edge { int u,v,w; bool operator < (const edge & a) const { return w < a.w; }} e[maxn*maxn];int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }inline void unionn(int x, int y) { fa[find(x)] = find(y); }inline void add(int x, int y, int w) { e[++tot].u = x; e[tot].v = y; e[tot].w = w; }int main() { read(n); for(register int i = 1; i <= n; i++) read(w), fa[i] = i, add(0, i, w); for(register int i = 1; i <= n; i++) for(register int j = 1; j <= n; j++) read(w), add(i, j, w); sort(e+1, e+tot+1); for(register int i = 1; i <= tot; i++) if(find(e[i].u) != find(e[i].v)) unionn(e[i].u, e[i].v), ans += e[i].w; printf("%d",ans); return 0;}
阅读全文
0 0
- 【BZOJ1601】【MST】灌水 题解
- 【bzoj1601】【Usaco2008 Oct】灌水 (最小生成树) 题解&代码
- [Bzoj1601][Usaco2008 Oct]灌水
- bzoj1601 [Usaco2008 Oct]灌水
- [BZOJ1601] [Usaco2008 Oct]灌水
- [BZOJ1601][Usaco2008 Oct]灌水
- bzoj1601[Usaco2008 Oct]灌水
- bzoj1601【Usaco2008 Oct】灌水
- BZOJ1601: [Usaco2008 Oct]灌水
- bzoj1601[Usaco2008 Oct]灌水
- [bzoj1601][Usaco2008 Oct]灌水
- bzoj1601 [Usaco2008 Oct]灌水
- BZOJ1601: [Usaco2008 Oct]灌水
- bzoj1601: [Usaco2008 Oct]灌水
- 【bzoj1601】灌水 最小生成树
- BZOJ1601 灌水(USACO08 Oct)
- bzoj1601 [Usaco2008 Oct]灌水(prim)
- 【BZOJ1601】[Usaco2008 Oct]灌水【最小生成树】
- 如何用Construct 2制作游戏
- redis GEO地理位置
- JAVA环境变量的配置
- default-autowire="byName" default-lazy-init="true"
- Git简单用法
- 【BZOJ1601】【MST】灌水 题解
- 多因素排序
- 父子bash的环境变量和自定义变量继承问题
- SSH知识点整理
- chapter8:聚类---群组发现
- datagridview中使最新增加的一行样式不一样 兼谈网友 skypaf 的问题
- LeetCode Exercise 4: Next Permutation
- 序列化与反序列化
- C++继承体系下的内存大小