做漫画网站,seo推广优化,电商网站建设小强,西安微信网站开发一、最小生成树
1.1Prim算法
朴素版Prim
一般用于稠密图
算法流程:
集合表示当前已经在连通块的点
1.初始化距离#xff0c;把所有距离都初始化为正无穷
2.n次迭代,找到集合外距离最小的点 -t
3.用t来更新其它点到集合的距离
#includeiostream
#include把所有距离都初始化为正无穷
2.n次迭代,找到集合外距离最小的点 -t
3.用t来更新其它点到集合的距离
#includeiostream
#includecstring
#includealgorithmusing namespace std;const int N 510,INF 0x3f3f3f3f;int n,m;
int g[N][N];
int dist[N];
bool st[N];int prim()
{memset(dsit,0x3f,sizeof dsit);int res 0;for(int i 0;i n;i ){int t -1;for(int j 1;j n;j ){if(! st[j] (t -1 || dist[t] dist[j]))t j;}if(i dist[t] INF) return INF;for(int j 1;j n;j ) dist[j] min(dist[j],g[t][j]);st[t] true;}return res;
}
int main()
{scanf(%d%d,n,m);memset(g,0x3f,sizeof g);while(m --){int a,b,c;scanf(%d%d%d,a,b,c);g[a][b] g[b][a] min(g[a][b],c);}int t prim();if(t INF) puts(impossible);else printf(%d\n,t);return 0;
} 1.2Kruskal算法
一般用于稀疏图
算法流程:
1.将所有边按照权重从小到大排序
2.枚举每一条边(a,b)权重为c
如果(a,b)不连通则将这条边加入集合中
#includeiostream
#includealgorithmusing namespace std;const int N 100010;int n,m;
//并查集的集合
int p[N];struct Edge
{int a,b,w;bool operator (const Edge W)const{return w W.w;}
}edges[N];int find(int x)
{if(p[x] ! x) p[x] find(p[x]);return p[x];
}
int main()
{scanf(%d%d,n,m);for(int i 0;i m;i ){int a,b,w;scanf(%d%d%d,a,b,w);edges[i] {a,b,w};}sort(edges,edges m);for(int i 1;i n;i ) p[i] i;int res 0,cnt 0;for(int i 0; i m; i ){//从小到大枚举所有边int a edges[i].a,b edges[i].b,w edges[i].w;//知道a与b的祖宗节点a find(a),b find(b);//判断a与b是否连通if(a ! b){//集合合并p[a] b;res w;cnt ;}}if (cnt n - 1) puts(impossible);else printf(%d\n,res);return 0;
}二、二分图
二分图当且仅当图中不含奇数环
2.1染色法
#includeiostream
#includecstring
#includealgorithmusing namespace std;const int N 100010,M 200010;int n,m;
int h[N],e[M],ne[M],idx;
int color[N];void add(int a,int b)
{e[idx] b,ne[idx] h[a],h[a] idx ;
}bool dfs(int u,int c)
{//当前点的颜色是ccolor[u] c;for(int i h[u];i ! -1;i ne[i]){int j e[i];if(!color[j]){if(!dfs(j,3 - c)) return false;}else if (color[j] c) return false;}return true;
}int main()
{scanf(%d%d,n,m);memset(h,-1,sizeof h);while(m --){int a,b;scanf(%d%d,a,b);add(a,b),add(b,a);}bool flag true;for(int i 1;i n;i ){if(!color[i]){if(!dfs(i,1)){flag false;break;}}}if(flag) puts(Yes);else puts(No);return 0;
}2.2匈牙利算法
#includeiostream
#includealgorithm
#includecstringusing namespace std;const int N 510,M 100010;int n1,n2,m;
int h[N],e[M],ne[M],idx;
int match[N];
bool st[N];void add(int a,int b)
{e[idx] b,ne[idx] h[a],h[a] idx ;
}
bool find(int x)
{for(int i h[x];i ! -1;i ne[i]){int j e[i];if(!st[j]){st[j] true;if(match[j] 0 || find(match[j])){match[j] x;return true;}}}return false;
}
int main()
{scanf(%d%d%d,n1,n2,m);memset(h,-1,sizeof h);while(m --){int a,b;scanf(%d%d,a,b);add(a,b);}int res 0;for(int i 0;i n1;i ){memset(st,false,sizeof st);if(find(i)) res ;}printf(%d\n,res);return 0;
}
文章转载自: http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.ggnkt.cn.gov.cn.ggnkt.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.c7507.cn.gov.cn.c7507.cn