POJ1358 Agri-Net
就是裸的最小生成树,复习一下。
prim算法:
G=(V,E),V是点集,E是边集
假设T=(U,TE)是最小生成树。U,TE初始化为空
首先从V中任取一点 假设取V1,然后U={V1},只要U是V的真子集,就从那些一个端点在T中,一个端点在T外的边中,找一条最短边。一直下去,直到找到n-1条边
找边使用 堆优化,复杂度(ElogV) 邻接表复杂度(V2)
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f; struct node{
int v,dis;
node(){}
node(int _v,int _dis){
v=_v;
dis=_dis;
}
bool operator<(const node &b) const {
return dis>b.dis;
}
}; int n,dis[maxn];
vector<node> G[maxn];
bool vis[maxn]; void prim() {
int ans=,num=;
priority_queue<node> pq;
for(int i=;i<n;i++) {
vis[i]=false;
dis[i]=inf;
}
pq.push(node(,));
node f;
while(num<n&&!pq.empty()) {
do{
f=pq.top();
pq.pop();
}while(vis[f.v]&&!pq.empty());
if(!vis[f.v]) {
ans+=f.dis;
vis[f.v]=true;
num++;
for(int i=;i<G[f.v].size();i++) {
int v=G[f.v][i].v;
if(!vis[v]) {
if(dis[v]>G[f.v][i].dis) {
dis[v]=G[f.v][i].dis;
pq.push(node(v,dis[v]));
}
}
}
}
}
if(num<n) printf("-1\n");//因为这里记录的是点的个数。不是边
else printf("%d\n",ans);
} int main() {
while(~scanf("%d",&n)) {
memset(G,,sizeof(G));
for(int i=;i<n;i++) {
for(int j=;j<n;j++) {
int x;
scanf("%d",&x);
if(x) G[i].push_back(node(j,x));
}
}
prim();
}
}
krusual算法:
将图G中的边按权值从小到大选取,使选取的不与生成树构成回路。直到n-1条边为止
判断回路就用并查集搞一下。
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f; int n;
struct Edge{
int u,v,dis;
Edge(){}
Edge(int _u,int _v,int _dis) {
u=_u;
v=_v;
dis=_dis;
}
bool operator <(const Edge &b) const {
return dis<b.dis;
}
}; vector<Edge> edge; int fa[maxn]; int Find(int x) {
if(fa[x]==-) return x;
else return fa[x]=Find(fa[x]);
} void Union(int x,int y) {
int fx=Find(x);
int fy=Find(y);
if(fx!=fy) {
fa[fx]=fy;
}
} int main() {
while(~scanf("%d",&n)) {
memset(fa,-,sizeof(fa));
edge.clear();
for(int i=;i<n;i++) {
for(int j=;j<n;j++) {
int x;
scanf("%d",&x);
if(x&&j<i) edge.push_back(Edge(i,j,x));
}
}
sort(edge.begin(),edge.end());
int num=,ans=;
for(int i=;i<edge.size();i++) {
int u=edge[i].u,v=edge[i].v,dis=edge[i].dis;
if(Find(u)!=Find(v)) {
num++;
ans+=dis;
Union(u,v);
}
}
if(num==n-) printf("%d\n",ans);
else printf("-1\n");
}
}
POJ1358 Agri-Net的更多相关文章
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- UI中经常出现的下拉框下拉自动筛选效果的实现
小需求是当你在第一个下拉框选择了国家时,会自动更新第二个省份的下拉框,效果如下 两个下拉选择Html如下: <select id="country_select"> & ...
- 告别无止境的增删改查:Java代码生成器
对于一个比较大的业务系统,我们总是无止境的增加,删除,修改,粘贴,复制,想想总让人产生一种抗拒的心里.那有什么办法可以在正常的开发进度下自动生成一些类,配置文件,或者接口呢? 有感于马上要做个比较 ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- 告别无止境的增删改查--Java代码生成器
转自:http://www.cnblogs.com/zhuYears/archive/2012/02/29/2373491.html 告别无止境的增删改查--Java代码生成器 有感于马上要做个比较大 ...
- 网络-05-端口号-F5-负载均衡设-linux端口详解大全--TCP注册端口号大全备
[root@test1:Standby] config # [root@test1:Standby] config # [root@test1:Standby] config # [root@test ...
- Mybatis中 Integer 值为0时,默认为空字符串的解决办法。
需求是查询级别为0的用户 User对象里的level字段的值为0,查询时居然没有查到为level为0的用户. <select id="selectSelective" par ...
- CentOS 7 服务端口表
# Note that it is presently the policy of IANA to assign a single well-known# port number for both T ...
- etymon word air aero aeri aer ag agreement walk joint trick skill chief forget out~1
1● air 2● aero 3● aeri 4● aer 空气 充气 1● ag 做,代理做 =====>agency 1● agr 2● agri 3 ...
随机推荐
- System.Security.Cryptography.CryptographicException: 出现了内部错误。
引用:http://www.cnblogs.com/ithome8/p/5189926.html 我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请 ...
- tomcat 7 7.0.73 url 参数 大括号 {} 不支持 , 7.0.67支持
7.0.73 url有JSON.stringify一个对象,然后作为参数拼接.结果请求报400错误,但是tomcat 7.0.67版本没有问题,猜测是高级版本对url参数比较严格. 处理方法: ...
- sqlplus客户端出现乱码
查询oracle server端的字符集 select userenv('language') from dual; 修改客户端 cmd是gbk编码 环境变量 NLS_LANG 值:SIMPLIFIE ...
- xcode添加build phase
[xcode添加build phase] xcode版本:5.0.2,找了半天,终于找到add build phase的方法,如下图.
- Vue基础以及指令, Vue组件
Vue基础篇一 Vue指令 Vue的指令directive很像我们所说的自定义属性,指令时Vue模板中最常用的功能,它带有v-前缀,功能是当表达式改变的时候,相应的行为作用在DOM上. <div ...
- 在aspx页动态加载ascx页面内容,给GridView控件绑定数据
在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...
- - description 方法作用
自定义一个Person类 @interface Person : NSObject { int _age; double _height; double _weight; NSString *_nam ...
- vector的简单用法
vector是C++中容器的一种,与普通的数组相比,它可以动态的增长,而且还有封装了用于顺序表的操作的方法. 使用vector定义了容器之后,如果定义了容器的大小,则可以在大小范围之内直接使用数组的方 ...
- c++ deque 容器
deque (全名 double ended queue)是一种具有队列和栈一样的数据结构. 在c++标准库中几乎和vector容器的接口完全相同,但它和vector 还是有一些细微的差别. 1. d ...
- 新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项
新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项. 在弹出的窗口中,下拉滚动条找到Server,并单击next按钮. 在弹出的窗口中,找到第一个A ...