Bzoj_3894_ Arts and Sciences Division &&bzoj_2127_happiness_ min cut

Source: Internet
Author: User

Bzoj_3894_ Science and Arts Division _ min Cut

Description Science and Arts Division is a very tangled matter! (Although people who see this topic certainly have not tangled up) small p in the class to carry out the arts and Sciences Division. His class can be described by a n*m matrix, each of which represents a classmate's seat. Each student must choose a subject from the arts and sciences. Students will get a satisfactory value when they choose the subject. The satisfaction value is obtained as follows: 1. If the classmate of line I second J chooses the liberal arts, he will get art[i][j] satisfaction value, if choose Science, will get SCIENCE[I][J] satisfaction value. 2. If the students in row J of line I chose the liberal arts, and he was next to each other (two squares and only if they had an identical edge), all the students chose the liberal arts, he would be happier, so he would increase same_art[i][j] satisfaction. 3.  If the students in row J of Line I chose science, and all of his classmates chose science, the satisfaction value of same_science[i]j[] was increased. Small p wants to know how people should choose, in order to make all people's satisfaction value and maximum. Please tell him the maximum value. Input first behavior two positive integers: N,m next n technique m Integer, representing Art[i][j]; next n the number of M integers. SCIENCE[I][J]; next n the M integer, which indicates that the same_art[i][j];output output is an integer representing the maximum satisfactory value of the sum of sample Input3 4
13 2 4 13
7 13 8 12
18 17 0 5

8 13 15 4
11 3 8 11
11 18 6 5

1 2 3 4
4 2 3 2
3 1 0 4

3 2 3 2
0 2 2 1
0 2 4 4Sample Output the The minimum cut model is converted to "no cut". Each person either learns the text or the theory, therefore s->x (A[x]), X->t (S[i]). For all combinations, create a new two node. S->P1 (sa[]), P1->x (INF), X->P2 (INF), P2->t (ss[]) then the total weight minus the minimum cut as the answer. Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 30050#define M 500050#define S (n*m+1) #define T (n*m+2) #define P (i,j) ((i-1) *m+j) #define INF 100000000int head[n],to[m<<1],nxt[ M<<1],cnt=1,n,m,dep[n],q[n],l,r,flow[m<<1],sum,a[105][105],b[105][105];int Tx[]={0,1,-1,0};int ty[]= {1,0,0,-1};inline void Add (int u,int v,int f) {to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; flow[cnt]=f;to[++cnt]=u; nxt[ CNT]=HEAD[V]; head[v]=cnt; flow[cnt]=0;} BOOL BFs () {memset (dep,0,sizeof (DEP)); int i; l=r=0; Q[r++]=s; Dep[s]=1;while (l<r) {int x=q[l++];for (I=head[x];i;i=nxt[i]) {if (!dep[to[i]]&&flow[i]) {dep[to[i]]=dep[x ]+1;if (to[i]==t) return 1; Q[r++]=to[i];}}} return 0;} int dfs (int x,int MF) {//puts ("FGVFIUGV"), if (x==t) return mf;int i,nf=0;for (I=head[x];i;i=nxt[i]) {if (dep[to[i]]==dep[ X]+1&&flow[i]) {int Tmp=dfs (to[i],min (Mf-nf,flow[i]));//if (!tmp) dep[to[i]]=0;nf+=tmp;flow[i]-=tmp;flow[i ^1]+=tmp;if (NF==MF) break;}} ReTurn NF;} void Dinic () {int f;while (BFS ()) while (F=dfs (S,inf)) sum-=f;printf ("%d\n", sum);} int main () {scanf ("%d%d", &n,&m), int i,j,x,k;for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &x); Sum+=x;add (S,p (I,J), x);}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &x); Sum+=x;add (P (i,j), t,x);}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &a[i][j]); sum+=a[i][j];}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {scanf ("%d", &b[i][j]); sum+=b[i][j];}} int Tot=n*m+2;for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {Tot+=2;add (s,tot-1,a[i][j]); Add (Tot,t,b[i][j]); Add ( Tot-1,p (I,J), INF), Add (P (i,j), tot,inf); for (k=0;k<4;k++) {int di=i+tx[k],dj=j+ty[k];if (di>=1&&di< =n&&dj>=1&&dj<=m) {Add (Tot-1,p (DI,DJ), INF), Add (P (DI,DJ), Tot,inf);}}}} Dinic ();} /*3 413 2 4 137 13 8 1218 17 0 58 13 15 411 3 8 1111 18 6 51 2 3 44 2 3 23 1 0 43 2 3 20 2 2 10 2 4 4 * *

Bzoj_3894_ Arts and Sciences Division &&bzoj_2127_happiness_ min cut

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.