Tidying Up

Time Limit: 4000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 316C1
64-bit integer IO format: %I64d      Java class name: (Any)

Smart Beaver is careful about 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 that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dressing room becomes a mess.

Smart Beaver from ABBYY is not only the brightest beaver in the area, but he also is 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 is much in to do and it’s the gym day, so he wants to clean up as soon as possible. Now the floors are washed, the dust is wiped off — it’s time to clean up in the dressing room. But as soon as the Smart Beaver entered the dressing room, all plans for the day were suddenly destroyed: chaos reigned there and it seemed impossible to handle, even in a week. Give our hero some hope: tell him what is the minimum number of shoes need to change the position to make the dressing room neat.

The dressing room is rectangular and is divided into n × m equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from 1 to , more formally, a square with coordinates (i, j) contains an integer number of the pair which is lying on it. The Smart Beaver believes that the dressing room is neat only when each pair of sneakers lies together. We assume that the pair of sneakers in squares (i1, j1) and (i2, j2) lies together if |i1 - i2| + |j1 - j2| = 1.

 

Input

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

It is guaranteed that:

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

The input limits for scoring 30 points are (subproblem C1):

  • 2 ≤ n, m ≤ 8.

The input limits for scoring 100 points are (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 Input

Input
2 3
1 1 2
2 3 3
Output
2
Input
3 4
1 3 2 6
2 1 5 6
4 4 5 3
Output
4

Source

 
解题:最小费用最大流
 
 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
const int maxn = ;
using namespace std;
struct arc {
int to,flow,next;
int cost;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
};
int head[maxn],tot,p[maxn],d[maxn];
arc e[];
bool in[maxn];
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool spfa(int S,int T) {
for(int i = ; i < maxn; i++) {
p[i] = -;
d[i] = INF;
in[i] = false;
}
d[S] = ;
queue<int>q;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]) {
q.push(e[i].to);
in[e[i].to] = true;
}
}
}
}
return p[T] > -;
} int calc(int S,int T) {
int tmp = ,mxV;
while(spfa(S,T)) {
mxV = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
mxV = min(mxV,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= mxV;
e[i^].flow += mxV;
tmp += e[i].cost*mxV;
}
}
return tmp;
}
int mp[][];
int main() {
int n,m,S,T;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
for(int i = tot = ; i < n; ++i)
for(int j = ; j < m; ++j) {
scanf("%d",mp[i]+j);
}
S = n*m;
T = S + ;
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) {
if((i+j)&) {
add(S,i*m+j,,);
if(i) add(i*m+j,(i-)*m+j,,mp[i][j] != mp[i-][j]);
if(j) add(i*m+j,i*m+j-,,mp[i][j] != mp[i][j-]);
if(i+ < n)
add(i*m+j,(i+)*m+j,,mp[i][j] != mp[i+][j]);
if(j+ < m)
add(i*m+j,i*m+j+,,mp[i][j] != mp[i][j+]);
} else add(i*m+j,T,,);
}
printf("%d\n",calc(S,T));
}
return ;
}

CodeForces 316c1 Tidying Up的更多相关文章

  1. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. 新疆大学OJ(ACM) 1047: string 字符串排序

    1047: string 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n个字符串字符串n<=50000,把所有字符串串起来,得到一个字典序最小的字符串. 输入 输入第一行是一 ...

  2. 读 Real-Time Rendering 收获 - chapter 6. texturing

    Texturing, at its simplest, is a techinique for efficiently modeling the surface's properties.

  3. Intellij IDEA 2018.3激活破解方法(解决key is invalid)

    1.程序安装包: https://download.jetbrains.8686c.com/idea/ideaIU-2018.3.exe 2.破解补丁:http://idea.lanyus.com/j ...

  4. web移动端-弹性盒模型

    (父元素加) : /*新版弹性盒模型*/ /* display: flex; */ /*设置主轴方向为水平方向*/ /* flex-direction: row; */ /*设置主轴方向为垂直方向*/ ...

  5. centos7 jumpserver 部署和使用手册(一)

    测试推荐环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: CentOS 7.2 IP: 192 ...

  6. 一句话木马和中国菜刀的结合拿webshell

    什么叫做一句话木马:     就是一句简单的脚本语言,一句话木马分为Php,asp,aspx等 中国菜刀:   连接一句话木马的工具 实验的目的:  通过一句话木马来控制我们的服务器,拿到webshe ...

  7. harbor 安装过程

    [root@master01 harbor]# ./prepare  Clearing the configuration file: ./common/config/adminserver/env ...

  8. Trustie站点代码托管使用指南

    "中国人的github"猛击Trustie官网,開始代码托管... 1.     新建项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  9. js面向对象编程:怎样定义常量?

    js中有一个keywordconst,但眼下的浏览器似乎还不支持,假设一定要定义一些常量,事实上能够使用闭包,匿名函数实现常量的定义. 比如: var Class = (function() { va ...

  10. hdu5249 Tricks Device(网络流最大匹配)

    分析题意可知: 1.最少须要切断多少边使吴不能找到张(题意吴仅仅能走最短路径上面的边),对从起点到终点的最短路径又一次建图,每条边的权值为1.求最大流就可以 2.在吴能够找到张的前提下,最多能够切断边 ...