BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)
题意
Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。 建造一个水库需要花费Wi(1<=Wi<=100000),连接两块土地需要花费Pij(1<=pij<=100000,pij=pji,pii=0). 计算Farmer John所需的最少代价。
思路
很经典的最小生成树模型……很久没做最小生成树一下子没想出来TAT……
首先得有个水源吧,设个虚节点当水源,然后连向每个土地,权值为建造水库的花费,其他的照常建图,然后求最小生成树就行了。
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
const int MAX = 305;
struct edge{
int v, w;
edge(){
}
edge(int _v, int _w){
v = _v;
w = _w;
}
};
struct MST{
vector <edge> adj[MAX];
int dist[MAX];
bool vis[MAX];
priority_queue <pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
void init(int n){
for (int i = 0; i <= n; i ++){
adj[i].clear();
vis[i] = false;
}
}
void add_edge(int u, int v, int w){
adj[u].push_back(edge(v, w));
adj[v].push_back(edge(u, w));
}
int solve(int s, int n){
for (int i = 0; i <= n; i ++) dist[i] = 0x3fffffff;
while(!PQ.empty()) PQ.pop();
dist[s] = 0;
PQ.push(make_pair(0, s));
while(!PQ.empty()){
int u = PQ.top().second;
PQ.pop();
vis[u] = true;
for (int i = 0; i < (int)adj[u].size(); i ++){
int v = adj[u][i].v, w = adj[u][i].w;
if (!vis[v] && dist[v] > w){
dist[v] = w;
PQ.push(make_pair(w, v));
}
}
}
int res = 0;
for (int i = 1; i <= n; i ++) res += dist[i];
return res;
}
}prim;
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
scanf("%d", &n);
prim.init(n+1);
for (int i = 1; i <= n; i ++){
int tmp;
scanf("%d", &tmp);
prim.add_edge(n+1, i, tmp);
}
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++){
int tmp;
scanf("%d", &tmp);
if (i >= j) continue;
prim.add_edge(i, j, tmp);
}
}
printf("%d\n", prim.solve(n+1, n+1));
return 0;
}
[/cpp]
BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)的更多相关文章
- BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点
Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- 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]灌水【最小生成树】
挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...
- BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)
题意: 300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价 思路: 先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑m ...
- BZOJ 1601: [Usaco2008 Oct]灌水( MST )
MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...
- Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...
- 1601: [Usaco2008 Oct]灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 1342 Solved: 881 [Submit][S ...
随机推荐
- MySQL Crash Course #16# Chapter 24. Using Cursors + mysql 循环
mysql中游标的使用案例详解(学习笔记)这篇讲得相当直白好懂了. 索引: cursor 基础讲解 mysql 循环 书上的整合代码 cursor 基础讲解 cursor 有点类似于 JDBC 中的 ...
- P1516/bzoj1477 青蛙的约会
青蛙的约会 exgcd 根据题意列出方程: 设所用时间为T,相差R圈时相遇 (x+T*m)-(y+T*n)=R*l 移项转换,得 T*(n-m)-R*l=x-y 设a=n-m,b=l,c=x-y,x_ ...
- 动态规划(Dynamic Programming)
introduction 大部分书籍介绍"动态规划"时,都会从"菲波纳切数列"讲起. 菲波纳切数列 递归解法 C++ 代码如下 unsigned long in ...
- elasticsearch分词器ik
1. 下载和es配套的版本 git clone https://github.com/medcl/elasticsearch-analysis-ik 2. 编译 cd elasticsearch-an ...
- 20145122《Java程序设计》第七周学习总结
教材学习内容总结 1.在只有Lambda表达式的情况下,参数的类型必须写出来. 2.Lambda表达式本身是中性的,同样的Lambda表达式可用来表示不同目标类型的对象操作. 3.Lambda表达式只 ...
- Android移植学习笔记
1.一头雾水不知道用什么编译环境,不知道用什么下载软件 编译软件:Ubuntu12.04(编译主机系统),JDK(Java) 下载软件: 模拟器: qemu
- SC命令---安装、开启、配置、关闭windows服务 bat批处理(转载)
转载:http://www.jb51.net/article/49627.htm 转载:http://blog.csdn.net/c1520006273/article/details/5053905 ...
- BZOJ2654: tree 二分答案+最小生成树
Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...
- Java中++,--,前缀后缀表达值的不同,与^的值计算
package 习题1; /** * ++ -- 与 ^ 的一些计算 * @author SeeClanUkyo * */ public class Test4 { public static voi ...
- Nlog、elasticsearch、Kibana以及logstash在项目中的应用(二)
上一篇说如何搭建elk的环境(不清楚的可以看我的上一篇博客http://www.cnblogs.com/never-give-up-1015/p/5715904.html),现在来说一下如何用Nlog ...