# Codeforces 316c1 Tidying Up

Source: Internet
Author: User

Tidying UpTime limit:4000msmemory limit:262144kbthis problem would be judged onCodeforces. Original ID:316C1
64-bit integer IO format: %i64d Java class name: (any)

Smart Beaver is careful on his appearance and pays special attention to shoes so he has a huge number of pairs of shoes From the most famous brands of the Forest. He ' s trying to handle his shoes carefully so, each pair stood side by side. But by the end of the week because of he very active lifestyle in his dressing the becomes a mess.

Smart Beaver from ABBYY are not only the brightest Beaver in the "area", but he also are the most domestically oriented. For example, on Mondays the Smart Beaver cleans everything in his home.

It ' s Monday morning. Smart Beaver does not want to spend the whole day cleaning, besides, there are much in to do and it's the gym day, so he WA NTS to clean up as soon as possible. Now the floors was washed, the dust is wiped off-it ' s time-to-clean up in the dressing. But as soon as the Smart Beaver entered the dressing-all-plans for the day were suddenly Destroyed:chaos reigned th Ere and it seemed impossible to handle, even in a week. Give our hero some hope:tell him what's the minimum number of shoes need to change the position-make the dressing roo M Neat.

The dressing are rectangular and is divided intoNXmEqual squares, each square contains exactly one shoe. Each pair of shoes have a unique number is an integer from1 To, more formally, a square with coordinates (i, J) contains an integer number of the pair WH Ich is lying on it. The Smart Beaver believes that the dressing was neat only if each pair of sneakers lies together. We assume the pair of sneakers in squares (i1, J1) and (i2, j< /c12>2) lies together if | I1- i2| + | J1- j2| = 1.

Input

The first line contains the space-separated integers n and M. They correspond to the dressing hostel size. Next n lines contain m space-separated integers each. Those numbers describe the dressing. Each number corresponds to a snicker.

It's Guaranteed that:

• N· m is even.
• All numbers, corresponding to the numbers of pairs of shoes in the dressing, would lie between 1 and.
• Each number from 1 to would occur exactly twice.

The input limits for scoring points is (Subproblem C1):

• 2 ≤ n, m ≤8.

The input limits for scoring points is (Subproblems c1+c2):

• 2 ≤ n, m ≤80.
Output

Print exactly one integer-the minimum number of the sneakers that need to change their location.

Sample InputInput
`2 31 1 22 3 3`
Output
`2`
Input
`3 41 3 2 62 1 5 64 4 5 3`
Output
`4`
SourceABBYY Cup 3.0 Problem Solving: Minimum cost maximum flow
`1#include <bits/stdc++.h>2 #defineINF 0x3f3f3f3f3 Const intMAXN =100010;4 using namespacestd;5 structArc {6     intTo,flow,next;7     intCost ;8Arcintx =0,inty =0,intz =0,intNXT =-1) {9to =x;TenFlow =y; OneCost =Z; ANext =NXT; -     } - }; the intHEAD[MAXN],TOT,P[MAXN],D[MAXN]; -Arc e; - BOOL inch[MAXN]; - voidAddintUintVintFlowintCost ) { +E[tot] =arc (V,flow,cost,head[u]); -Head[u] = tot++; +E[tot] = arc (U,0,-cost,head[v]); AHEAD[V] = tot++; at } - BOOLSPFA (intSintT) { -      for(inti =0; i < MAXN; i++) { -P[i] =-1; -D[i] =INF; -         inch[I] =false; in     } -D[s] =0; toqueue<int>Q; + Q.push (S); -      while(!Q.empty ()) { the         intU =Q.front (); * Q.pop (); \$         inch[U] =false;Panax Notoginseng          for(inti = Head[u]; ~i; i =E[i].next) { -             if(E[i].flow && d[e[i].to] > D[u] +e[i].cost) { theD[e[i].to] = D[u] +E[i].cost; +P[e[i].to] =i; A                 if(!inch[e[i].to]) { the Q.push (e[i].to); +                     inch[E[i].to] =true; -                 } \$             } \$         } -     } -     returnP[t] >-1; the } - Wuyi intCalcintSintT) { the     intTMP =0, MxV; -      while(SPFA (s,t)) { WuMxV =INF; -          for(inti = p[t]; ~i; i = p[e[i^1].to]) AboutMxV =min (mxv,e[i].flow); \$          for(inti = p[t]; ~i; i = p[e[i^1].to]) { -E[i].flow-=MxV; -e[i^1].flow + =MxV; -TMP + = e[i].cost*MxV; A         } +     } the     returntmp; - } \$ intmp[ -][ -]; the intMain () { the     intn,m,s,t; the      while(~SCANF ("%d%d",&n,&m)) { thememset (head,-1,sizeofhead); -          for(inti = tot =0; I < n; ++i) in              for(intj =0; J < M; ++j) { thescanf"%d", mp[i]+j); the             } AboutS = nm; theT = S +1; the          for(inti =0; I < n; ++i) the              for(intj =0; J < M; ++j) { +                 if((I+J) &1) { -Add (S,i*m+j,1,0); the                     if(i) Add (I*m+j, (i)1) *m+j,1, mp[i][j]! = mp[i-1][j]);Bayi                     if(j) Add (i*m+j,i*m+j-1,1, mp[i][j]! = mp[i][j-1]); the                     if(i+1<N) theAdd (I*m+j, (i+1) *m+j,1, mp[i][j]! = mp[i+1][j]); -                     if(j+1<m) -Add (i*m+j,i*m+j+1,1, mp[i][j]! = mp[i][j+1]); the}ElseAdd (I*m+j,t,1,0); the             } theprintf"%d\n", Calc (s,t)); the     } -     return 0; the}`
View Code

Codeforces 316c1 Tidying Up

Related Keywords:

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.