The main idea: to give an no-map, to find the minimum tree shape.
Topic Analysis: Set Zhu-Liu algorithm template on the line ...
The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;# define LL Long long# define REP (i,s,n) for (int i=s;i<n;++i) # define CL (b) memset (A,b,sizeof (a)) # define CLL (A, B , n) Fill (a,a+n,b) const int N=1005;CONST int inf=1<<30;struct edge{int fr,to,w;}; Edge e[n*40];int vis[n],id[n],pre[n],in[n],n,m;int zhu_liu (int root,int nv,int ne) {int res=0; while (1) {CLL (IN,INF,NV); REP (I,0,ne) if (E[I].FR!=E[I].TO&&IN[E[I].TO]>E[I].W) {IN[E[I].TO]=E[I].W; pre[e[i].to]=e[i].fr; } in[root]=0; REP (I,0,NV) if (in[i]==inf) return-1; int nodecnt=0; CL (id,-1); CL (vis,-1); REP (I,0,NV) {res+=in[i]; int v=i; while (vis[v]!=i&&id[v]==-1&&v!=root) {vis[v]=i; V=PRE[V]; } if (V!=root&&id[v]==-1) {for (int u=pre[v];u!=v;u=pre[u]) id[u]=nodecnt; id[v]=nodecnt++; }} if (nodecnt==0) break; REP (I,0,NV) if (id[i]==-1) id[i]=nodecnt++; REP (i,0,ne) {int v=e[i].to; E[i].fr=id[e[i].fr]; E[I].TO=ID[E[I].TO]; if (e[i].fr!=e[i].to) e[i].w-=in[v]; } nv=nodecnt; Root=id[root]; } return res; int main () {int t,cas=0; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); REP (i,0,m) {scanf ("%d%d%d", &E[I].FR,&E[I].TO,&E[I].W); } int Ans=zhu_liu (0,N,M); printf ("Case #%d:", ++cas); if (ans<0) printf ("possums!\n"); else printf ("%d\n", ans); } return 0;}
UVA-11183 Teen Girl Squad (minimum tree diagram, Zhu Liu algorithm template)