Network flow/min cut
Rausen is too god spicy ~ as a konjac Konjac or the handling of it ...
It is obvious that a network flow problem.
First of all, add up all the values, and then reduce the network flow minimum cut value is good, the problem is how to build a diagram. This seems to have been tested many times ...
To abstract each person into a point P, then
First S-to-p connection, flow for the selected arts of joy, p to the T-edge, the flow of science to choose the happy value.
Then is the condition of same, to each person new two points P1, p2
S to P1, traffic for the liberal arts same, P1 to neighboring points and their P-side, the flow of the INF
P2 phase T connecting edge, the flow is the joy value of the science same, adjacent points and their P-to-P2 edge, the traffic is INF
Then run the network stream just fine (konjac konjac tot = 1 did not write the tune for a long while thought the map was wrong = =b)
1 /**************************************************************2 problem:38943 User:tunix4 language:c++5 result:accepted6 time:2404 Ms7 memory:40340 KB8 ****************************************************************/9 Ten //Bzoj 3894 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at intv=0, sign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; - } - Const intn=1e6+Ten, m=2000010, inf=~0u>>2; intypedefLong LongLL; - /******************tamplate*********************/ to Const intfx[]={0,1,0,-1,0}, +fy[]={1,0,-1,0,0}; - intN,m,tot,ans; the structedge{intto,v;}; *InlineintPackintIintj) {return(I-1) *m+J;} $ #defineFor f (i,1,n) f (j,1,m)Panax Notoginseng structnet{ - Edge E[m]; the inthead[n],next[m],cnt; + voidInsintXintYintv) { Ae[++cnt]=(Edge) {y,v}; theNEXT[CNT]=HEAD[X]; head[x]=CNT; + } - voidAddintXintYintv) { $Ins (x,y,v); Ins (y,x,0); $ } - intS,t,cur[n],d[n],q[n]; - BOOLMklevel () { thememset (d,-1,sizeofd); -d[s]=0;Wuyi intL=0, r=-1; theq[++r]=s; - while(l<=R) { Wu intx=q[l++]; - for(intI=head[x];i;i=Next[i]) About if(d[e[i].to]==-1&&e[i].v) { $d[e[i].to]=d[x]+1; -q[++r]=e[i].to; - } - } A returnd[t]!=-1; + } the intDfsintXinta) { - if(x==t)returnA; $ intflow=0; the for(intI=head[x];i && flow<a;i=Next[i]) the if(E[I].V && d[e[i].to]==d[x]+1){ the intF=dfs (E[i].to,min (A-flow,e[i].v)); thee[i].v-=F; -e[i^1].v+=F; inflow+=F; the } the if(!flow) d[x]=-1; About returnflow; the } the voidDinic () { the while(Mklevel ()) ans-=DFS (s,inf); + } - voidinit () { theN=getint (); M=getint (); Cnt=1;BayiTot=n*m; s=0; t=tot*3+1; the intx=0; the for{ -X=getint (); ans+=x; - Add (S,pack (I,J), x); the } the for{ theX=getint (); ans+=x; the Add (Pack (I,J), t,x); - } the for{ theX=getint (); ans+=x; theAdd (s,tot+Pack (I,J), x);94F (k,0,4){ the inttx=i+fx[k],ty=j+Fy[k]; the if(tx<1|| ty<1|| tx>n| | TY>M)Continue; theAdd (tot+Pack (I,J), pack (tx,ty), INF);98 } About } - for{101X=getint (); ans+=x;102Add (tot*2+Pack (i,j), t,x);103F (k,0,4){104 inttx=i+fx[k],ty=j+Fy[k]; the if(tx<1|| ty<1|| tx>n| | TY>M)Continue;106Add (Pack (tx,ty), tot*2+Pack (i,j), INF);107 }108 }109 dinic (); theprintf"%d\n", ans);111 } the }g1;113 the intMain () { the #ifndef Online_judge theFreopen ("3894.in","R", stdin);117Freopen ("3894.out","W", stdout);118 #endif119 g1.init (); - return 0;121}
View Code
"Bzoj" "3894" Science and arts Division