BZOJ3996[TJOI2015] Linear algebra

Source: Internet
Author: User

Description

Give a n*n matrix B and a 1*n matrix C. Find a 1*n 01 matrix A to make

D= (a*b-c) *a^t max. Where A^t is the transpose of a. Output dinput The first line input an integer n, the next n line input B matrix, the first row of the J number represents bij. The next line is to enter n integers, representing the matrix C. Each number in matrix B and Matrix C is a non-negative integer that does not exceed 1000. Output

Output the largest D

Sample Input3
1 2 1
3 1 0
1 2 3
2 3 7Sample Output2HINT

1<=n<=500

Exercises

This is a good packing. ...

D=a*b*a^t-c*a^t

If clause I of a is 1, then D subtracts c[1,i].

If Item I and item J of a are all 1, then D will add b[i,j].

In this way, the title becomes a model similar to BZOJ1497[NOI2006 's maximum profit: N items, taking an item will have a price, and if you take two items at the same time, it will be profitable.

The problem becomes the maximal weight closed sub-graph, and the minimum cut solution is obtained by Dinic network flow.

Code:

Constinf=100000000;typeRec=RecordS,e,w,flow,next:longint; End;varb,bb,d,q:Array[0..2002002] ofLongint; A:Array[-1..2002000] ofRec; V:Array[0..2002000] ofBoolean; N,m,i,j,k,l,st,ed,ww,top,tar,ans,x:longint;functionmin (aa,bb:longint): Longint;begin  ifAa<bb Thenexit (AA); exit (BB);End;procedureAdd (st,ed,ww:longint);beginInc (top); A[TOP].S:=St; A[TOP].E:=Ed; A[TOP].W:=ww; A[top].next:=B[st]; B[ST]:=top;End;functionBfs:boolean;varHead,tail,x,u:longint; Y:rec;beginFillchar (v,sizeof (v), false); Tail:=1; head:=0; d[st]:=1; V[ST]:=true; q[1]:=St;  whileHead<tail Do  beginInc (head); x:=Q[head]; U:=B[x];  whileU>0  Do    beginy:=A[u]; if( notV[Y.E]) and(Y.FLOW&LT;Y.W) Then      beginV[Y.E]:=true; D[Y.E]:=d[x]+1; Inc (tail); Q[tail]:=y.e; End; U:=Y.next; End; End; Exit (V[tar]);End;functionAddflow (p,maxflow:longint): Longint;varO:longint; Y:rec;begin  if(P=tar)or(maxflow=0) Thenexit (Maxflow); Addflow:=0;  whileBb[p]>0  Do  beginy:=A[bb[p]]; if(d[y.e]=d[p]+1) and(Y.FLOW&LT;Y.W) Then    begino:=addflow (Y.e,min (maxflow,y.w-y.flow)); ifO>0  Then      beginInc (A[BB[P]].FLOW,O); Dec (a[bb[p] XOR1].flow,o);        Dec (maxflow,o);        Inc (ADDFLOW,O); ifmaxflow=0  ThenBreak ; End; End; BB[P]:=Y.next; End;End;functionNetwork:longint;beginNetwork:=0;  whileBFs Do  begin     forI:=st toTar Dobb[i]:=B[i];  Inc (Network,addflow (St,inf)); End;End;beginREADLN (n); ST:=0; Top:=1; tar:=N;  fori:=1  toN Do   forj:=1  toN Do  beginread (x); ifX>0  Then    beginInc (TAR);      Add (st,tar,x); Add (Tar,st,0);      Add (Tar,i,inf); Add (I,tar,0);      Add (Tar,j,inf); Add (J,tar,0); Ans:=ans+x; End; End;  Inc (TAR);  fori:=1  toN Do  beginRead (j);    Add (I,TAR,J); Add (Tar,i,0); End; Writeln (ans-network);End.
View Code

BZOJ3996[TJOI2015] Linear algebra

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.