题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1601

题意:

  Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记。

  把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库。

  建造一个水库需要花费wi(1<=wi<=100000),连接两块土地需要花费P[i][j](1 <= p[i][j] <= 100000, p[i][j]=p[j][i], p[i][i]=0)。

  计算Farmer John所需的最少代价。

题解:

  农田标号1到n,建立超级源点为0号点。

  从超级源点向每个农田连一条长度为w[i]的边。然后n个农田之间再互相连接,边长为p[i][j]。

  然后求最小生成树就好。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 305 using namespace std; struct Edge
{
int sour;
int dest;
int len;
Edge(int _sour,int _dest,int _len)
{
sour=_sour;
dest=_dest;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len<b.len;
}
}; int n;
int ans;
int par[MAX_N];
vector<Edge> edge; void init_union_find()
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} bool same(int x,int y)
{
return find(x)==find(y);
} int kruskal()
{
init_union_find();
sort(edge.begin(),edge.end());
int cnt=;
int res=;
for(int i=;i<edge.size();i++)
{
Edge temp=edge[i];
if(!same(temp.sour,temp.dest))
{
cnt++;
res+=temp.len;
unite(temp.sour,temp.dest);
}
}
return cnt==n?res:-;
} void read()
{
cin>>n;
int a;
for(int i=;i<=n;i++)
{
cin>>a;
edge.push_back(Edge(,i,a));
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>a;
if(i<j) edge.push_back(Edge(i,j,a));
}
}
} void solve()
{
ans=kruskal();
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 1601 [Usaco2008 Oct]灌水:最小生成树的更多相关文章

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

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

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

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

  3. BZOJ 1601 [Usaco2008 Oct]灌水

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

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

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

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

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

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

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

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

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

  8. Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole

    题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...

  9. 1601: [Usaco2008 Oct]灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1342  Solved: 881 [Submit][S ...

随机推荐

  1. java编译命令工具javac

    Reads Java class and interface definitions and compiles them into bytecode and class files. Synopsis ...

  2. Koa2 + Mongoose + Log4js 持久化日志

    代码地址如下:http://www.demodashi.com/demo/12466.html  之前做的项目是采用 Express 框架进行搭建的,其中的日志管理采用了 winston + Post ...

  3. mixare的measureText方法在频繁调用时抛出“referencetable overflow max 1024”的解决方式

    这几天在搞基于位置的AR应用,採用了github上两款开源项目: mixare android-argument-reality-framework 这两个项目实现机制大致同样.我选取的是androi ...

  4. 转:RC复位电路的原理及其复位时间的计算

    RC复位电路的原理及其复位时间的计算   低电平有效复位电路如下 此复位电路是针对低电平有效复位而言的,其中二极管是起着在断电的情况下能够很快的将电容两端的电压释放掉,为下次上电复位准备. 假设电容两 ...

  5. Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect

    Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect ...

  6. ios 推送证书没有密钥 解决方案【转载】

    注意事项: 1.keychains选择Login 2.2.在创建完CertificateSigningRequest.certSigningRequest可以看到Keys中该有你的私有秘钥 3.按文档 ...

  7. Spring MVC学习纲要

    感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...

  8. python 基础 8.1 r 正则对象

                                                                                                        ...

  9. Something Starts While Something Ends

    (1)最终还是没能参加比赛,一次都没有机会. (2)有梦想,不到最后一刻不会放弃. (3)这里应该会搬次家,转到github上. (4)作为一个新手,什么东西都需要从头学起来,就从最基础的数据结构开始 ...

  10. substr扩展版:支持中文字符串截取

    function d_substr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_e ...