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 ...
随机推荐
- STM32f103C8T6 Bootloader设计(转)
源:STM32f103C8T6 Bootloader设计 STM32F103c8t6通过串口实现IAP在线升级固件
- Python入门之Python的单例模式和元类
一.单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在. 当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上 ...
- 浏览器内核、排版引擎、js引擎
[定义] 浏览器最重要或者说核心的部分是“Rendering Engine”,可大概译为“渲染引擎”,不过我们一般习惯将之称为“浏览器内核”.负责对网页语法的解释(如标准通用标记语 言下的一个应用HT ...
- npm 查看全局安装过的包
查看全局安装的包 npm list -g --depth 0 非全局安装的包 npm list --depth 0 如果不加参数 --depth 0会显示安装的包以及相关的依赖包,会显示的很详细.
- tensorflow reduction_indices理解
在tensorflow的使用中,经常会使用tf.reduce_mean,tf.reduce_sum等函数,在函数中,有一个reduction_indices参数,表示函数的处理维度,直接上图,一目了然 ...
- Python3基础 os listdir 列举指定的所有文件及文件夹的名字
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【监控】dubbo监控中心安装
使用dubbo的话,两个工具是不可少的: 1:dubbo的管理控制台,在之前的笔记中介绍过 2:简易控制中心monitor 简单介绍下monitor: Simple Monitor挂掉不会影响到Con ...
- 通过java代码对kylin进行cube build
转:http://www.cnblogs.com/hark0623/p/5580632.html 通常是用于增量 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...
- Python非递归遍历多叉树
class Queue: def __init__(self,max_size): self.max_size = int(max_size) self.queue = [] def put(self ...
- POJ 1029 False coin
http://poj.org/problem?id=1029 题意: 在一堆硬币中有一个假硬币,重量是重是轻不知道.每次称量多个硬币,并给出称量结果.判断依据题目给出的几次称量结果能否找出假硬币. 思 ...