hdu4009 the smallest tree-shaped drawing board sub-problem

Source: Internet
Author: User

/*
Adjusted the smallest tree diagram of the afternoon, yesterday just read the template.
The smallest tree diagram, is the smallest generation of graphs, it's magical = =
*/
#include <iostream>#include<cstring>#include<cstdio>#include<cmath>#defineMAXN 1002#defineINF 0x3f3f3f3fusing namespacestd;structnode{intx, Y, Z;} NODES[MAXN];structedge{intU,v,w;} E[MAXN*MAXN];inttot;intDistintAintBintYintz) {    intres=0; Res+=abs (nodes[a].x-nodes[b].x) *y+abs (NODES[A].Y-NODES[B].Y) *y+abs (nodes[a].z-nodes[b].z) *y; if(nodes[a].z<nodes[b].z) Res+=Z; returnRes;}intPRE[MAXN],ID[MAXN],VISIT[MAXN],inch[MAXN];intZhuliu (intRootintNintm) {    intres=0, U,v;  while(1){         for(intI=0; i<n;i++)            inch[i]=INF; //Set up the minimum arc set e         for(intI=0; i<m;i++)            if(E[I].U!=E[I].V && e[i].w<inch[E[I].V]) {PRE[E[I].V]=e[i].u; inch[e[i].v]=E[I].W; }         for(intI=0; i<n;i++)            if(I!=root &&inch[i]==INF)return-1;//There is no minimum tree chart                inttn=0; memset (ID,-1,sizeofID); memset (Visit,-1,sizeofvisit); inch[root]=0;  for(intI=0; i<n;i++) {//looking for ring dyeingres+=inch[i]; V=i;  while(Visit[v]!=i && id[v]==-1&& v!=root) {Visit[v]=i; V=Pre[v]; }//looking for a ring            if(V!=root && id[v]==-1){                 for(intu=pre[v];u!=v;u=Pre[u]) Id[u]=TN; ID[V]=tn++; }//Dyeing        }        if(tn==0)//No ring             Break;  for(intI=0; i<n;i++)//give the Dianran color without the ring            if(id[i]==-1) Id[i]=tn++;  for(intI=0;i<m;) {//v=e[i].v; E[I].U=ID[E[I].U]; E[I].V=ID[E[I].V]; if(E[I].U!=E[I].V)//if it's in a different ring .E[I++].W-=inch[v]; ElseSwap (e[i],e[--m]); } N=tn;//The remaining TN pointsroot=Id[root]; }    returnRes;}intMain () {intn,x,y,z,k,v;  while(SCANF ("%d%d%d%d", &n,&x,&y,&z)!=eof &&N) {Tot=0;  for(intI=1; i<=n;i++) scanf ("%d%d%d",&nodes[i].x,&nodes[i].y,&nodes[i].z);  for(intu=1; u<=n;u++) {scanf ("%d",&k);  while(k--) {scanf ("%d",&v); E[TOT].U=u; E[TOT].V=v; E[TOT].W=Dist (U,V,Y,Z); Tot++; }        }                introot=0;  for(intI=1; i<=n;i++) {e[tot].u=Root; E[TOT].V=i; E[TOT].W=x*nodes[i].z; Tot++; } printf ("%i64d\n", Zhuliu (root,n+1, tot)); }    return 0;}

hdu4009 the smallest tree-shaped drawing board sub-problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.