Will a[i] with him behind the small side of the building, and then seek the maximum density sub-graph
#include <iostream>#include<algorithm>#include<string.h>#include<cstdio>#include<vector>#include<queue>#include<cmath>using namespacestd;Const intmaxn= the;Const Doubleeps=0.00000001;Const Doubleinf=100000005; Vector<int>ans;structdinic{structEdge {int from, to; DoubleCap,flow; Edge (intCfrom=0,intcto=0,Doubleccap=0,Doublecflow=0) { from=cfrom; To=cto; Cap=ccap; flow=Cflow; } }; intn,m,s,t; Vector<Edge>edges; Vector<int>G[MAXN]; BOOLVIS[MAXN]; intD[MAXN]; intCUR[MAXN]; voidInitintN) { This->n=N; M=0; Edges.clear (); for(intI=0; i<=n; i++) g[i].clear (); } voidAddedge (int from,intTo,Doublecap) {Edges.push_back (Edge ( from, To,cap,0)); Edges.push_back (Edge to, from,0,0)); M+=2; g[ from].push_back (M-2); G[to].push_back (M-1); } BOOLBFS () {memset (Vis,false,sizeof(VIS)); Queue<int>p; Q.push (s); D[s]=0; Vis[s]=1; while(!Q.empty ()) { intx=Q.front (); Q.pop (); for(intI=0; I<g[x].size (); i++) {Edge&e =Edges[g[x][i]]; if(vis[e.to]==false&&e.cap>e.flow) {vis[e.to]=1; D[e.to]=d[x]+1; Q.push (e.to); } } } returnVis[t]; } DoubleDFS (intXDoublea) {if(x==t| | a==0)returnA; Doubleflow=0, F; for(int&i=cur[x]; I<g[x].size (); i++) {Edge&e=Edges[g[x][i]]; if(d[x]+1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {E.flow+=F; Edges[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(a==0) Break; } } returnflow; } DoubleMaxflow (intSintt) { This->s=s; This->t=T; Doubleflow=0; while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S,inf); } returnflow; }}t;intD[MAXN];inta[maxn*maxn],b[maxn*MAXN];intN[MAXN];DoubleU;voidBuildintNintMDoubleg) {T.init (n+2); for(intI=1; i<=n; i++) {T.addedge (n+1, I,u); T.addedge (I,n+2, u+g*2-D[i]); } for(intI=0; i<m; i++) {T.addedge (a[i],b[i],1); T.addedge (B[i],a[i],1); }}intMain () {intCAs; scanf ("%d",&CAs); for(intCc=1; cc<=cas; Cc++) { intN; scanf ("%d",&N); for(intI=1; i<=n; i++) scanf ("%d",&N[i]); intm=0; memset (d,0,sizeof(d)); for(intI=1; i<=n; i++) for(intj=i+1; j<=n; J + +) if(n[i]>N[j]) {D[n[i]]+ +;d [n[j]]++; A[M]=n[i]; B[M]=N[J]; m++; } if(m==0) {printf ("Case #%d:0.0000000\n", CC); Continue; } DoubleL=0, r=m; U=m; while(r-l>=EPS) { DoubleMid= (L+R) *0.5; Build (N,m,mid); DoubleAns=t.maxflow (n+1, n+2); Ans= (U*n-ans) *0.5; if(ans>0) l=mid; ElseR=mid; } printf ("Case #%d:%.7lf\n", cc,l); } return 0;}/*253 4 2 5 1 3 4 5*/
View Code
2014 XI ' an Division C question