【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601
很水的题,但是一开始我看成最短路了T_T
果断错。
我们想,要求连通,对,连通!连通的价值最小!当然是生成树!最小生成树!
边的还好做,但是这题有点,怎么办呢?
因为点在图中也起到连通作用,我们加个附加源,向每个点连边,价值为对应点权。
!
然后就ok了。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
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; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=500;
int p[N], cnt, n;
struct ED { int u, v, w; } e[N*N+N];
const bool cmp(const ED &a, const ED &b) { return a.w<b.w; }
void add(const int &u, const int &v, const int &w) {
e[++cnt].u=u; e[cnt].v=v; e[cnt].w=w;
}
const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }
int main() {
read(n);
for1(i, 1, n) add(0, i, getint());
int fx, fy, ans=0;
for1(i, 1, n) for1(j, 1, n) {
read(fx);
if(i!=j) add(i, j, fx);
}
sort(e+1, e+1+cnt, cmp);
for1(i, 1, n) p[i]=i;
for1(i, 1, cnt) {
fx=ifind(e[i].u); fy=ifind(e[i].v);
if(fx!=fy) {
p[fx]=fy;
ans+=e[i].w;
}
}
print(ans);
return 0;
}
Description
Input
Output
Sample Input
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
Sample Output
输出详解:
Farmer John在第四块土地上建立水库,然后把其他的都连向那一个,这样就要花费3+2+2+2=9
HINT
Source
【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)的更多相关文章
- BZOJ 1601 [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...
- BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...
- BZOJ——1601: [Usaco2008 Oct]灌水
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: ...
- BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)
题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...
- BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点
Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- BZOJ 1601: [Usaco2008 Oct]灌水( MST )
MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...
- BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)
题意: 300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价 思路: 先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑m ...
- bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】
挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...
- 1601: [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 1342 Solved: 881 [Submit][S ...
随机推荐
- 2014.7建兰NOIP模拟Day1 Running
突然间翻到着题,想想那时的我真是垃圾,这么简单的tarjan缩点+树上倍增都不会..还想了3h+.. 什么时候写了它吧...
- http协议学习系列
深入理解HTTP协议(转) http://www.blogjava.net/zjusuyong/articles/304788.html http协议学习系列 1. 基础概念篇 1.1 介绍 H ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- linux 创建连接命令 ln -s 软链接
这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s, 具体用法是:ln -s 源文件 目标文件. 当 我们需要在不同 ...
- php封装练习
<body> <?php class jisuan { private $a; private $b; function __construct($a,$b) { $this-> ...
- h5 canvas 画图
h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 小甲鱼PE详解之区块描述、对齐值以及RVA详解(PE详解06)
各种区块的描述: 很多朋友喜欢听小甲鱼的PE详解,因为他们觉得课堂上老师讲解的都是略略带过,绕得大家云里雾里~刚好小甲鱼文采也没课堂上的教授讲的那么好,只能以比较通俗的话语来给大家描述~ 通常,区块中 ...
- SQLServer备份脚本
企业管理器--管理--SQL Server代理--右键作业--新建作业--"常规"项中输入作业名称--"步骤"项--新建--"步骤名"中输入 ...
- pigcms 标签读不出
错误:{pigcms:jc['portrait']} 正确:{pigcms:$jc['portrait']}