POJ 3164 Minimum tree diagram template (priority C + +, g++ sometimes wa==)

Source: Internet
Author: User
Tags cmath

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>#define EPS1e-8UsingNamespace Std;/*Minimum tree graph template-Zhu Liu algorithmTemplate Description: Point marking must be 0-(N-1)Must be removed to its own point (the Benquan of its own side is infinitely large)*/#define M109#define TypeDoubleConstTypeInf=1e20;struct Node{int UV;TypeCost;}e[M*m+5];int Pre[M],id[M],Vis[M];TypeInch[M];Type Directed_mst(int root,int NV,int NE){TypeRet=0;While(True){1. Find the smallest entry edgeFor(int I=0; I<nv; I+ +) in[I]= inf;For(int I=0; I<ne; I++){int U= E[I].u;Int V= E[I].v;If(E[I].cost< in[V]&& u! = V){Pre[V]= u; in[V]= E[I].cost;}}For(int I=0; I<nv; I++){If(I= = Root)Continue;If(In[I]= = inf)Return-1;The root cannot reach it except with a little in the side.}2. Find a ringint Cntnode=0;Memset(ID,-1,sizeof(ID));Memset(Vis,-1,sizeof(Vis)); in[Root]=0;For(int I=0; I<nv; i++) {//mark each ring ret + = 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] = Cntnode;} ID[V] = Cntnode + +;}} if (Cntnode = = 0) break;//no ring for (int i=0;i<nv;i++) if (id[i] = = 1) {Id[i] = Cntnode + +;} 3. Indent, re-mark for (int i=0;i<ne;i++) {int 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) {e[i].cost-= In[v];}} NV = Cntnode;root = Id[root];} return ret;} int n,m;struct tpoint{double x,y;void in () {scanf ("%lf%lf", &x,&y);}} P[M];d ouble dis (tpoint a,tpoint b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} void Init () {for (int i=0;i<n;++i) p[i].in (); int h=0;for (int i=0;i<m;++i) {int x,y;scanf ("%d%d", &x,&y); if (x==y) continue;x--;y--; E[h].u=x; E[h].v=y; E[h++].cost=dis (P[x],p[y]);} Double Ans=directed_mst (0,n,h), if (Ans==-1) puts ("poor Snoopy"); elseprintf ("%.2f\n", ans);} int main () {while (scanf ("%d%d", &n,&m)!=eof) {init();} return 0;}



I got a copy. = = g++ wa C + + AC
#include <iostream>#include<cstring>#include<Set>#include<map>#include<cmath>#include<stack>#include<queue>#include<deque>#include<list>#include<algorithm>#include<stdio.h>#include<iomanip>#defineRep (i,n) for (int i=0;i<n;++i)#defineFab (i,a,b) for (int i=a;i<=b;++i)#defineFBA (I,b,a) for (int i=b;i>=a;--i)#definePB push_back#defineMP Make_pair#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineSF scanf#definePF printf#defineLL Long LongConst intn= the;using namespaceStd;typedef pair<int,int>PII;#defineType DoubleConstType INF =1e20;#defineEPS 1e-8structedge{intu,v; Type cost;} E[n*n+Ten];intpre[n],id[n],vis[n];type in[n];type MST (intRootintNintm) {type RET=0;  while(true) {Rep (i,n) in[i]=inf; Rep (i,m) {intu=e[i].u; intv=e[i].v; if(E[i].cost < in[v] && u!=v) {Pre[v]=u; IN[V]=E[i].cost; }} Rep (i,n) {if(I==root)Continue; if(In[i]==inf)return-1; }        intCntnode=0; memset (ID,-1,sizeofID); memset (Vis,-1,sizeofvis); In[root]=0; Rep (i,n) {ret+=In[i]; intv=i;  while(vis[v]!=i&&id[v]==-1&&v!=root) {Vis[v]=i; V=Pre[v]; }            if(v!=root&&id[v]==-1){                 for(intu=pre[v];u!=v;u=Pre[u]) {Id[u]=Cntnode; } Id[v]=cntnode++; }        }        if(cntnode==0) Break; Rep (I,n)if(id[i]==-1) id[i]=cntnode++; Rep (i,m) {intv=e[i].v; E[I].U=ID[E[I].U]; E[I].V=ID[E[I].V]; if(E[I].U!=E[I].V) e[i].cost-=In[v]; } N=Cntnode; Root=Id[root]; }    returnret;}intn,m;structpoint{Doublex, y; void inch() {SF ("%LF%LF",&x,&y); }}p[n];DoubleDis (point a,point b) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}voidInit () {Rep (i,n) p[i].inch(); intH=0; Rep (i,m) {intx, y; SF ("%d%d",&x,&y); if(x==y)Continue; X--;y--; E[H].U=x; E[H].V=y; E[h++].cost=dis (p[x],p[y]); }    DoubleAns=mst (0, n,h); if(ans<0) puts ("Poor Snoopy"); Elsepf"%.2lf\n", ans+EPS);}intMain () { while(SF ("%d%d", &n,&m)! =EOF)    {init (); }    return 0;}

POJ 3164 Minimum tree diagram template (priority C + +, g++ sometimes wa==)

Related Article

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.