//KRUSCAL #include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn=2010,maxm=10010; struct node{ int x,y,w; }; struct node a[maxm],tmp; int f[maxn],rank[maxn]; void qsort(int l,int r){ int mid=a[(l+r)/2].w,i,j; i=l;j=r; while(i<j){ while(i<=j && a[i].w<mid)i++; while(i<=j && a[j].w>mid)j--; if(i<=j){ tmp=a[i];a[i]=a[j];a[j]=tmp; i++;j--; } } if(l<j)qsort(l,j); if(i<r)qsort(i,r); } int fa(int u){ if(f[u]!=u) f[u]=fa(f[u]); return f[u]; } int find(int u,int v){ return fa(u)==fa(v)?0:1; } void link(int u,int v){ if(rank[fa(u)]>=rank[fa(v)]){ f[fa(v)]=fa(u); rank[fa(u)]++; }else{ f[fa(u)]=fa(v); rank[fa(v)]++; } } int main(){ #ifndef ONLINE_JUDGE freopen("prim.in","r",stdin); // freopen("prim.out","w",stdout); #endif int i,j,k,m,n,s; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)f[i]=i; for(i=1;i<=m;i++) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); qsort(1,m); s=n-1;i=0; int ans=0; while(s && i<=m){ i++; if(find(a[i].x,a[i].y)){ link(a[i].x,a[i].y); s--; ans=ans>a[i].w?ans:a[i].w; } } printf("%d\n",ans); return 0; }
PRIM(N^2)
#include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn=2010,maxm=10010,INF=1000001000; int a[maxn][maxn],d[maxn],p[maxn]; int main(){ #ifndef ONLINE_JUDGE freopen("prim.in","r",stdin); //freopen("prim.out","w",stdout); #endif int i,j,k,m,n,s; int x,y,w,ans=0,min; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)d[i]=INF; for(i=1;i<=n;i++) for(j=1;j<=n;j++) a[i][j]=INF; for(i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&w); if(a[x][y]>w)a[x][y]=a[y][x]=w; } d[1]=0; for(i=1;i<=n;i++){ min=INF; for(j=1;j<=n;j++) if(!p[j] && d[j]<min){ min=d[j]; k=j; } p[k]=1; ans=ans>d[k]?ans:d[k]; for(j=1;j<=n;j++) if(a[k][j] && d[j]>a[k][j]) d[j]=a[k][j]; } printf("%d\n",ans); return 0; }
PRIM NLOG2N
#include<stdio.h> #include<string.h> #include<stdlib.h> const int Maxn=2010,Maxm=50010,Inf=1000010000; int e,to[Maxm],ws[Maxm],begin[Maxn],next[Maxm]; int d[Maxn],p[Maxn],q[Maxn],pos[Maxn]; void add(int x,int y,int z){ to[++e]=y; next[e]=begin[x]; begin[x]=e; ws[e]=z; } int main(){ //freopen("prim.in","r",stdin); //freopen("prim.out","w",stdout); int i,j,k,m,n,l; int x,y,z,u,v,w; scanf("%d%d",&n,&m); for(i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } for(i=1;i<=n;i++)d[i]=Inf; d[1]=0;l=n; for(i=1;i<=n;i++){q[i]=i;pos[i]=i;} int ans=0; for(i=1;i<=n;i++){ k=q[1]; q[1]=q[l];pos[q[l]]=1;l--; u=1; while( (u*2<=l && d[q[u]]>d[q[2*u]]) || (u*2+1<=l && d[q[u]]>d[q[2*u+1]]) ){ v=u*2; if(v+1<=l && d[q[v]]>d[q[v+1]])v++; w=q[u];q[u]=q[v];q[v]=w; w=pos[q[u]];pos[q[u]]=pos[q[v]];pos[q[v]]=w; u=v; } p[k]=1; ans=ans>d[k]?ans:d[k]; for(j=begin[k];j;j=next[j]){ u=to[j]; if(!p[u] && d[u]>ws[j])d[u]=ws[j]; v=pos[u]; while(v>=1 && d[q[v]]<d[q[v/2]]){ w=q[v];q[v]=q[v/2];q[v/2]=w; w=pos[q[v]];pos[q[v]]=pos[q[v/2]];pos[q[v/2]]=w; v=v/2; } } } printf("%d\n",ans); return 0; }