【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 ...
随机推荐
- 【Python】Python AES 对称加密示例
代码: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex AES_SECRET_KEY = ...
- SSHPASS支持从命令行输入密码
参考:http://www.2cto.com/os/201307/227911.html 手动下载地址:http://sourceforge.net/projects/sshpass/ 安装示例: w ...
- Android 转载一篇.9图片详解文章
感谢作者,原文链接为 http://blog.csdn.net/ouyang_peng/article/details/9242889
- Objective-C 和 C++中指针的格式和.方法 和内存分配
最近在看cocos2d-x,于是打算复习一下C++,在这里简单对比下,留个念想. 先看看oc中指针的用法 @interface ViewController : UIViewController { ...
- Java for LeetCode 078 Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- mybatis随机生成可控制主键的方式
mybatis生成的主键,一般都是用数据库的序列,可是还有不同的写法,比如: 一.NUMBER类型的主键 <insert id="insertPeriodical" para ...
- plsql查询数据显示为乱码解决方法
使用plsql查询数据显示为乱码: 查看数据库编码: 通过网上搜索,发现需要设置环境变量,添加以下环境变量: LANG=zh_CN.GBK NLS_LANG="SIMPLIFIED CHIN ...
- 一台机器开2个Tomcat修改端口号
修改一个Tomcat端口号步骤:1.找到Tomcat目录下的conf文件夹2.进入conf文件夹里面找到server.xml文件3.打开server.xml文件4.在server.xml文件里面找到下 ...
- hadoop配置文件加载顺序(转)
原文 http://www.cnblogs.com/wolfblogs/p/4147485.html 用了一段时间的hadoop,现在回来看看源码发现别有一番味道,温故而知新,还真是这样的 在使用h ...