In Figure (a), V1 indicates the source of water. Other N-1 nodes in the Figure indicate the farms we need to irrigate. An edge represents you can build a channel between the two nodes, to irrigate the target. The integers indicate the cost of a channel between two nodes.
Figure (b) represents a design of channels with minimum cost.
There are multiple cases, the first line of each case contains two integers N and M (2 ≤ N ≤ 100; 1 ≤ M ≤ 10000), N shows the number of nodes. The following M lines, each line contains three integers i j cij, means we can build a channel from node Vi to node Vj, which cost cij. (1 ≤ i, j ≤ N; i ≠ j; 1 ≤ cij ≤ 100)
The source of water is always V1.
The input is terminated by N = M = 0.
For each case, output a single line contains an integer represents the minimum cost.
If no design can irrigate all the farms, output "impossible" instead.
5 8 1 2 3 1 3 5 2 4 2 3 1 5 3 2 5 3 4 4 3 5 7 5 4 3 3 3 1 2 3 1 3 5 3 2 1 0 0
17 6
Problem setter: Hill
Source: TJU Contest August 2006
题目:http://acm.tju.edu.cn/toj/showp2248.html
分析:典型的最小树形图,直接做就行,由于这题边的长度很小,直接用桶排,算法真正做到O(V^2),刷到Rank 1,激动阿~~~不过之前忘记判断是否存在,wa6次,囧
代码:
#include<cstdio> using namespace std; const int mm=22222; const int mn=222; struct edge { int s,t,w; }g[mm],h[mm]; int head[mm],next[mm]; int p[mn],q[mn],mark[mn],fp[mn],from[mn],vis[mn],in[mn],w[mn],ans,sum; int i,j,k,n,m,e,r,mw; bool huan; inline void addedge(int u,int v,double c) { g[e].s=u,g[e].t=v,g[e].w=c,next[e]=head[u],head[u]=e++; if(c>mw)mw=c; } void dfs(int u) { ++sum,vis[u]=1; for(int i=head[u];i>=0;i=next[i]) if(!vis[g[i].t])dfs(g[i].t); } inline void init(int& a) { char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); for (a=0; ch>='0'&&ch<='9'; ch=getchar()) a=a*10+ch-48; } void mysort() { int i; for(i=0;i<=n;++i)q[i]=0; for(i=0;i<=mw;++i)p[i]=0; for(i=0;i<e;++i)++q[g[i].t],++p[g[i].w]; for(i=1;i<=n;++i)q[i]+=q[i-1]; for(i=n;i>0;--i)q[i]=q[i-1]; q[0]=0; for(i=1;i<=mw;++i)p[i]+=p[i-1]; for(i=mw;i>0;--i)p[i]=p[i-1]; p[0]=0; for(i=0;i<e;++i)h[p[g[i].w]++]=g[i]; for(i=0;i<e;++i)g[q[h[i].t]++]=h[i]; } int main() { while(init(n),init(m),n+m) { for(i=e=mw=0;i<=n;++i)head[i]=-1; while(m--) { init(i),init(j),init(k); if(i!=j)addedge(i,j,k); } for(sum=i=0;i<=n;++i)vis[i]=0; dfs(1); if(sum<n) { printf("impossible\n"); continue; } mysort(); for(i=0;i<=n;++i)fp[i]=p[i]=-1,in[i]=vis[i]=0,mark[i]=i; for(i=0;i<e;++i) if(p[g[i].t]<0)p[g[i].t]=i; huan=1,ans=sum=0; while(huan) { huan=0; for(i=2;i<=n;++i) if(fp[j=mark[i]]>=0) { if(fp[i]<0)in[i]+=w[j],mark[i]=mark[mark[i]]; else { in[i]+=w[i],ans+=w[i]; if(g[++p[fp[i]]].t!=fp[i])p[fp[i]]=-1; } } for(i=0;i<=n;++i)fp[i]=-1,vis[i]=0; for(i=2;i<=n;++i) if(p[i]>=0) { if(fp[j=mark[i]]<0||(fp[j]>=0&&w[j]>g[p[i]].w-in[i])) w[j]=g[p[i]].w-in[i],fp[j]=i,from[j]=mark[g[p[i]].s]; } for(sum=0,i=2;i<=n;++i) if(fp[i]>=0)sum+=w[i]; for(i=2;i<=n;++i) if(!vis[i]) { r=0,j=i; while(j>0&&vis[j]>=0) { if(vis[j]>0) { huan=1; while(q[--r]!=j)mark[q[r]]=j,vis[q[r]]=-1; vis[j]=-1; } else if(!vis[j])vis[q[r++]=j]=1; if(fp[j]>=0)j=from[j]; else j=-1; } while(r--)vis[q[r]]=fp[q[r]]=-1; } } printf("%d\n",ans+sum); } return 0; }
普通版本:
#include<cstdio> #define type int using namespace std; const int mm=11111; const int mn=111; const int oo=1000000000; int s[mm],t[mm],c[mm]; int id[mn],pre[mn],q[mn],vis[mn]; type in[mn],w[mn]; type Directed_MST(int root,int NV,int NE) { type ret=0,sum=0; int i,j,u,v,r; bool huan=1; for(i=0;i<=NV;++i)in[i]=0,id[i]=i,pre[i]=-1; while(huan) { for(i=0;i<=NV;++i) if(pre[j=id[i]]>=0) { if(pre[i]<0)in[i]+=w[j],id[i]=id[j]; else in[i]+=w[i],ret+=w[i]; } for(i=0;i<=NV;++i)pre[i]=-1,vis[i]=0; for(i=0;i<NE;++i) if((u=id[s[i]])!=(v=id[t[i]])&&(w[v]>(j=c[i]-in[t[i]])||pre[v]<0)) pre[v]=u,w[v]=j; for(i=1;i<=NV;++i) if(i!=root&&id[i]==i&&pre[i]<0)return -1; for(pre[root]=-1,sum=i=0;i<=NV;++i) if(pre[i]>=0)sum+=w[i]; for(huan=i=0;i<=NV;++i) if(!vis[i]) { r=0,j=i; while(j>=0&&vis[j]>=0) { if(vis[j]>0) { while(q[--r]!=j)id[q[r]]=j,vis[q[r]]=-1; huan=1,vis[j]=-1; } else vis[q[r++]=j]=1,j=pre[j]; } while(r--)vis[q[r]]=pre[q[r]]=-1; } } return ret+sum; } int main() { int i,n,m; while(scanf("%d%d",&n,&m),n+m) { for(i=0;i<m;++i)scanf("%d%d%d",&s[i],&t[i],&c[i]); type ans=Directed_MST(1,n,m); if(ans<0)printf("impossible\n"); else printf("%d\n",ans); } return 0; }