题面: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的更多相关文章

  1. BZOJ 1601 [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Description Farmer John已经决定把水灌到他的n(1 ...

  2. BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601 题意: Farmer John已经决定把水灌到他的n(1<=n<=300 ...

  3. BZOJ——1601: [Usaco2008 Oct]灌水

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit:  ...

  4. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...

  5. BZOJ 1601: [Usaco2008 Oct]灌水 最小生成树_超级源点

    Description Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. ...

  6. BZOJ 1601: [Usaco2008 Oct]灌水( MST )

    MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...

  7. BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)

    题意: 300个坑,每个坑能从别的坑引水,或者自己出水,i从j饮水有个代价,每个坑自己饮水也有代价,问让所有坑都有谁的最少代价 思路: 先建一个n的完全图,然后建一个超级汇点,对每个点连w[i],跑m ...

  8. bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】

    挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...

  9. luogu P1550 [USACO08OCT]打井Watering Hole

    题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastur ...

随机推荐

  1. codeblocks添加编译选项

    -std=c++0x

  2. pom文件报错org.apache.maven.archiver.mavenarchiver.getmanifest

    eclipse导入新的maven项目时,pom.xml第一行报错: org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.mav ...

  3. zabbix3.2使用fping批量监控ip的连通性

    .在zabbix-agent端安装fping wget http://www.fping.org/dist/fping-3.16.tar.gz tar zxvf fping-3.16.tar.gz c ...

  4. java接口自动化基础知识(一)

    一.TestNG+MySQL+MyBatis实现对测试用例数据的读取 本篇文章讲解TestNG+MySQL+MyBatis+ExtentReports实现对测试用例数据的读取,下面放出所有配置文件的目 ...

  5. Apache:SSLCertificateFile:文件不存在或为空(操作系统RHEL7)

    配置参数及报错信息(配置了TLS加密后httpd服务无法启动) 我在httpd.conf文件中引用了上面创建的文件.但在那之后,当我尝试RESTART Apache时,我遇到以下错误: 错误:SSLC ...

  6. CSS之垂直对齐

    vertical-align: baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text ...

  7. 金蝶K/3 报销相关SQL语句

    金蝶K/3 报销相关SQL语句 use AIS20180607113701 select fopenid,* from dbo.t_XunTong_User where Fname ='' go us ...

  8. Hive SemanticException

    hive, sex='boy'); FAILED: SemanticException Line : Invalid path ''/root/data/hdata.txt'': No files m ...

  9. Visual Studio 2017

    美国西雅图时间 3 月 7 日上午 9 点(北京时间 8 日凌晨 1 点),微软将正式发布 Visual Studio 2017.   下载地址:https://www.visualstudio.co ...

  10. Android - JSON Parser Tutorial

    Android provides four different classes to manipulate JSON data. These classes are JSONArray,JSONObj ...