Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole
题面:P1550 [USACO08OCT]打井Watering Hole
题解:无
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
int N,W[maxn],fa[maxn],num_edge=,edge_head[maxn];
ll Z,ans;
struct Edge{int from,to,nx;ll dis;}edge[*maxn*maxn];
inline void Add_edge(int from,int to,ll dis){
edge[++num_edge].nx=edge_head[from];
edge[num_edge].from=from;
edge[num_edge].to=to;
edge[num_edge].dis=dis;
edge_head[from]=num_edge;
return;
}
inline int getf(int n){
if(fa[n]==n)return n;
fa[n]=getf(fa[n]);
return fa[n];
}
inline bool cmp(const Edge&a,const Edge&b){return a.dis<b.dis;}
void Kruskal(){
int cnt=;
for(int i=;i<=N;i++)fa[i]=i;
sort(edge+,edge+num_edge+,cmp);
for(int i=;i<=num_edge;i++){
int x=edge[i].from,y=edge[i].to;
int fx=getf(x),fy=getf(y);
if(fa[x]!=fa[y]){
cnt++;
fa[fx]=fy;
ans+=edge[i].dis;
}
}
return;
}
int main(){
scanf("%d",&N);
for(int i=;i<=N;i++)scanf("%d",&W[i]);
for(int i=;i<=N;i++)Add_edge(,i,W[i]);
for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
scanf("%lld",&Z);
Add_edge(i,j,Z);
}
Kruskal();
printf("%lld\n",ans);
return ;
}
By:AlenaNuna
Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole的更多相关文章
- 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标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...
- 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& ...
- luogu P1550 [USACO08OCT]打井Watering Hole
题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...
随机推荐
- 简易解说拉格朗日对偶(Lagrange duality)
引言:尝试用最简单易懂的描述解释清楚机器学习中会用到的拉格朗日对偶性知识,非科班出身,如有数学专业博友,望多提意见! 1.原始问题 假设是定义在上的连续可微函数(为什么要求连续可微呢,后面再说,这里不 ...
- SQL Server - ISNULL
ISNULL 使用指定的替换值替换 NULL. 语法 ISNULL ( check_expression , replacement_value ) 参数 check_express ...
- 10. linux输入子系统/input 设备【转】
转自:https://www.cnblogs.com/crmn/articles/6696819.html 按键事件信息之上报绝对事件信息之上报相对事件信息之上报功能键驱动编写多点触控事件的上报 只产 ...
- AWK如何打印从某一列到最后一列的内容
awk -F " " '{for (i=4;i<=NF;i++)printf("%s ", $i);print ""}' file
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- css中的宽度
浏览器通过CSS对元素的盒子模型的描述进行页面渲染的.因此,元素的宽度受到父元素.css描述的影响. 通常,元素的宽度是指盒子模型中content-box所占用的宽度.也就是说,默认box-sizin ...
- James Munkres Topology: Theorem 19.6
Theorem 19.6 Let \(f: A \rightarrow \prod_{\alpha \in J} X_{\alpha}\) be given by the equation \[ f( ...
- MyCat-简介
一MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件.运行在代码应用和MySQL数据库之间的应用. 前身 : corba. 是阿里开发的数据库中间件.实现M ...
- Service的启动过程
--摘自<Android进阶解密> 第一步:ContextImpl到ActivityManagerService的调用过程 第二步:ActivityThread启动Service 1)Pr ...
- 【mybatis】-- springboot整合mybatis
1.添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>m ...