Title Address: Tju 2248
Minimum tree diagram template problem. Be familiar with the template.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)//#pragma comment (linker, "/stack:1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=40000+Ten;intCNT, N;intin[ the], color[ the], id[ the], pre[ the];structnode{intU, V, W;} edge[20000];voidAddintUintVintW) {edge[cnt].v=v; Edge[cnt].u=u; Edge[cnt++].w=w;}intD_mst (intRoot) {intans=0, I, u, v; while(1){memset(In,inf,sizeof(in)); for(i=0; i<cnt;i++) {u=edge[i].u; V=EDGE[I].V;if(U!=v&&edge[i].w<in[v]) {IN[V]=EDGE[I].W; Pre[v]=u; } } for(i=1; i<=n;i++) {if(In[i]==inf&&i!=root)return-1; }inttot=0;memset(color,-1,sizeof(color));memset(id,-1,sizeof(ID)); in[root]=0; for(i=1; i<=n;i++) {ans+=in[i]; U=i; while(color[u]!=i&&id[u]==-1&&u!=root) {color[u]=i; U=pre[u]; }if(id[u]==-1&&u!=root) {Id[u]=++tot; for(V=pre[u];v!=u;v=pre[v]) {Id[v]=tot; } } }if(!tot) Break; for(i=1; i<=n;i++) {if(id[i]==-1) Id[i]=++tot; } for(i=0; i<cnt;i++) {v=edge[i].v; EDGE[I].U=ID[EDGE[I].U]; EDGE[I].V=ID[EDGE[I].V];if(EDGE[I].U!=EDGE[I].V) {EDGE[I].W-=IN[V]; }} N=tot; Root=id[root]; }returnAns;}intMain () {intM, U, V, W, ans; while(scanf("%d%d", &n,&m)!=eof&&n+m) {cnt=0; while(m--) {scanf("%d%d%d", &u,&v,&w);if(U==V)Continue; Add (U,V,W); } ans=d_mst (1);if(ans==-1)puts("Impossible");Else printf("%d\n", ans); }return 0;}
Tju 2248 Channel Design (Minimum tree shape-Zhu Liu algorithm)