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 ...
随机推荐
- Hadoop之HDFS(三)HDFS的JAVA API操作
HDFS的JAVA API操作 HDFS 在生产应用中主要是客户端的开发,其核心步骤是从 HDFS 提供的 api中构造一个 HDFS 的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS ...
- day63-webservice 03.解析cxf提供的例子
Path配置: C:\Program Files (x86)\ScanSign;E:\app\zhongzh\product\11.2.0\dbhome_1\bin;D:\app\zhongzh\pr ...
- mongo_2 $in 和 $all 区别
in 只需满足( )内的某一个值即可, 而$all 必须满足[ ]内的所有值, > db.table1.find({}); { "_id" : ObjectId(" ...
- Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案
1.配置web.config <system.web> <sessionState mode="StateServer" stateConnectionStrin ...
- post get 方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- ASP.NET MVC Controller 编程所涉及到的常用属性成员
Controller (System.Web.Mvc.Controller) 1.获取路由中的各个值 Request.RequestContext.RouteData.Values["id& ...
- SQL 全角半角转换-(摘抄)
/****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...
- html5 video使用autoplay属性时,声音混乱
html5 video使用autoplay属性时,声音混乱 页面代码 Index.html <html xmlns="http://www.w3.org/1999/xhtml" ...
- WebStrom背景色设置
Ctrl Alt S快速打开setting:
- 和Webbrowser进行简单交互
作为第一篇,简单的控件使用就不说了. 直接从简单的交互开始吧! C#使用网页中已有的js函数 webBrowser.Document.InvokeScript("Stop");// ...