From the original, can be introduced d=σ (i=1,n,σ (j=1,n,a[i]*a[j]*b[i][j))-σ (I=1,n,a[i]*c[i]), so the method is as follows: from the source point like the first layer of n*n points, edge right for B[i][j], from the first layer like the second layer of the edge, Benquan is infinite, from the second layer to the sink point edge, Edge right c[i]. The final answer is σ (B)-maxflow.
Derivation process:
(A * b-c) * at
(1*n) (n*n) (1*n) (n*1)
=a*b * At-c * at
(1*n) (n*1) (1*1)
Make p[i][j]=σ (K=1,n,a[i][k]*b[k][j])
At[i][j]=a[j][i]=a[i] (j=1)
Native =σ (i=1,n,σ (j=1,n,a[i]*b[i][j]*a[j))-σ (I=1,n,a[i]*c[i])
Reference Hzwer Blog: http://hzwer.com/6814.html
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<algorithm>#include<queue>using namespacestd;template<Const int_n>structedge{structEdge_base {intTo,next,w;} E[_n]; intp[_n],cnt; Edge () {clear ();} intStartConst intx) {returnp[x];} voidInsertConst intXConst intYConst intz) {e[++cnt].to=y; E[CNT].NEXT=P[X]; E[cnt].w=z; p[x]=cnt;return ; } voidClear () {cnt=1, Memset (P,0,sizeof(p)); } edge_base&operator[](Const intx) {returne[x];};intsss,ttt,cur[2100000];intn,tot,level[2100000]; Edge<2100000>e;BOOLBfs (Const intS) { inti,t; Queue<int>Q; Memset (Level,0,sizeof(int) * (n+n*n+5)); Level[s]=1; Q.push (S); while(!Q.empty ()) {T=Q.front (); Q.pop (); for(I=e.start (t); i;i=E[i].next) { if(!level[e[i].to] &&E[I].W) {Level[e[i].to]=level[t]+1; Q.push (e[i].to); } } } returnlevel[ttt];}intDfs (Const intSConst intBK) { if(S==TTT)returnBK; intrest=BK; for(int&i=cur[s];i;i=E[i].next) { if(level[e[i].to]==level[s]+1&&E[I].W) { intflow=Dfs (E[i].to,min (REST,E[I].W)); E[I].W-=flow; E[i^1].w+=flow; if((Rest-=flow) <=0) Break; } } if(bk==rest) level[s]=0; returnbk-rest;}intDinic () {intflow=0; while(Bfs (SSS)) {memcpy (CUR,E.P,sizeof(int) * (n+n*n+5)); Flow+=dfs (SSS,0x3f3f3f3f); } returnflow;}intGetint () {intData=0; CharCh=GetChar (); while(ch<'0'|| Ch>'9') ch=GetChar (); while(ch>='0'&& ch<='9') data=data*Ten+ch- -, ch=GetChar (); returndata;}intMain () {inti,j,x,sum=0; N=Getint (); Tot=n; sss=tot+n*n+1; ttt=sss+1; for(i=1; i<=n;++i) { for(j=1; j<=n;++j) {x=getint (); + +tot; E.insert (SSS,TOT,X); E.insert (Tot,sss,0); E.insert (Tot,i,0x3f3f3f3f); E.insert (I,tot,0); E.insert (Tot,j,0x3f3f3f3f); E.insert (J,tot,0); Sum+=x; } } for(i=1; i<=n;++i) {x=Getint (); E.insert (I,TTT,X); E.insert (Ttt,i,0); } printf ("%d\n", sum-dinic ()); return 0;}
[TJOI2014] [Bzoj3996] linear algebra [network flow, Min Cut]