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 ...
随机推荐
- 主攻ASP.NET MVC4.0之重生:Asp.Net MVC WebApi OData
1.新建MVC项目,安装OData Install-Package Microsoft.AspNet.WebApi.OData -Version 4.0.0 2.新建WebAPI Controller ...
- windows 常用快捷键和dos命令
快捷键 win + R 打开dos命令行窗口 win + E 打开资源管理窗口 (计算机) shift + 鼠标右击 + select 在此处打开命令窗口 可在资源管理目录下打开dos命令 windo ...
- git2
1,开源的代码管理工具 2,分布式管理工具(更安全,可以脱网操作) 3,git的分支管理更加便捷. 4,代码的传输更新速度更快 利用git可以进入多人配合代码开发.有备份.协同 sudo apt-ge ...
- 函数引用参数加const
Fun(const Type& type); 在引用传递的时候,在函数内部改变参数,会改变参数实际值. 加上了const就不能被修改.
- Razor语法快速参考
语法/示例 Razor Web Forms对应写法或说明 代码块 @{ int x = 123; string y = "because.";} <% int x = 123 ...
- 字符编码(ASCII、ANSI、GB2312、UTF-8等)系统梳理(转载)
引言 在显示器上看见的文字.图片等信息在电脑里面其实并不是我们看见的样子,即使你知道所有信息都存储在硬盘里,把它拆开也看不见里面有任何东西,只有些盘片.假设,你用显微镜把盘片放大,会看见盘片表面凹凸不 ...
- mongodb 有一个坑 报错 no mongos proxies found in seed list
mongoose 的报当我从 mongoose@4.5.2 升级到 mongoose@4.6.5的时候,出现了一个问题: Unhandled rejection MongoError: no mong ...
- 试着用React写项目-利用react-router解决跳转路由等问题(二)
转载请注明出处:王亟亟的大牛之路 这一篇还是继续写react router相关的内容,废话之前先安利:https://github.com/ddwhan0123/Useful-Open-Source- ...
- 【Tomcat部署】Linux环境部署war包到tomcat
以turbine为例. 一.部署 1.下载或者生成war包(从maven上下载war包,并改名字为turbine.war) 2.将turbine.war拷贝到$TOMCAT_HOME/webapps中 ...
- 12_Python操作MySQL(basic)
""" Test connection to MySQL using mysql-client conn = MySQLdb.connect(host,port,user ...