Hud 1102 Constructing Roads [Kruscal]
来源:互联网 发布:java poi是什么意思 编辑:程序博客网 时间:2024/06/04 18:32
题目链接:击点
#include<stdio.h>#include<string.h>#include<algorithm>#define N 105#define Max 6000#define MAX 10000using namespace std;int Map[N][N],nodeset[N];bool vis[N];struct Rode{ int x,y,w;}r[Max];bool cmp(Rode a,Rode b){ if(a.w<b.w) return true; return false;}int Kruscal(int n,int k){ int top=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i<j) { r[top].x=i; r[top].y=j; r[top].w=Map[i][j]; top++; } } } sort(r,r+top,cmp); int m=k,min=0; top=0; while(m<n-1) { if(!vis[r[top].x]&&vis[r[top].y]) { m++; vis[r[top].x]=true; nodeset[r[top].x]=nodeset[r[top].y]; min+=r[top].w; } else if(vis[r[top].x]&&!vis[r[top].y]) { m++; vis[r[top].y]=true; nodeset[r[top].y]=nodeset[r[top].x]; min+=r[top].w; } if(!vis[r[top].x]&&!vis[r[top].y]) { m++; vis[r[top].x]=true; vis[r[top].y]=true; nodeset[r[top].x]=nodeset[r[top].y]; min+=r[top].w; } else { if(nodeset[r[top].x]!=nodeset[r[top].y]) { m++; int tmp=nodeset[r[top].x]; for(int z=0;z<=n;z++) if(tmp==nodeset[z]) nodeset[z]=nodeset[r[top].y]; min+=r[top].w; } } top++; } return min;}void Init(int n){ memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) Map[i][j]=MAX; for(int i=0;i<=n;i++) nodeset[i]=i;}int main(){ int n; while(~scanf("%d",&n)) { Init(n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&Map[i][j]); int k,BIT=0; scanf("%d",&k);//修建的路可能重复修建. for(int i=1;i<=k;i++) { int a,b; scanf("%d%d",&a,&b); if(!vis[a]&&vis[b]) { BIT++; vis[a]=true; nodeset[a]=nodeset[b]; } else if(vis[a]&&!vis[b]) { BIT++; vis[b]=true; nodeset[b]=nodeset[a]; } else if(!vis[a]&&!vis[b]) { BIT++; vis[a]=true; vis[b]=true; nodeset[a]=nodeset[b]; } else { if(nodeset[a]!=nodeset[b]) { BIT++; int tmp=nodeset[a]; for(int x=0;x<=n;x++) if(nodeset[x]==tmp) nodeset[x]=nodeset[b]; } } Map[a][b]=MAX; Map[b][a]=MAX; } printf("%d\n",Kruscal(n,BIT)); }}
Kruscal 写的太挫了,需要努力精简代码啊。
- Hud 1102 Constructing Roads [Kruscal]
- Constructing Roads HDU杭电1102【Kruscal || Prim】
- Nyoj 434&&Hud 1301 Jungle Roads[最小生成树Kruscal]
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- Hdu-1102 Constructing Roads
- Hdu-1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- hd 1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- Hdu 1102 - Constructing Roads
- 如何学习Unity3D
- 查找最小的K个元素(快速排序法思路)
- 系统上安装了多种浏览器,能否指定某浏览器访问指定页面?请说明原由。
- spring中的bean与java中的bean区别
- Java compiler level does not match the version of the installed Java project facet.
- Hud 1102 Constructing Roads [Kruscal]
- ASIHTTPRequest-Cookie的使用
- opencv源码解析之(6):hog源码分析
- 晒晒长春的IT
- ASIHTTPRequest-数据压缩
- 这些天都一版一版的来的。
- linux用户管理命令
- CMFCToolTipInfo选件类
- 单片机驱动段式液晶屏