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. (转载)Android之三种网络请求解析数据(最佳案例)

    [置顶] Android之三种网络请求解析数据(最佳案例) 2016-07-25 18:02 4725人阅读 评论(0) 收藏 举报  分类: Gson.Gson解析(1)  版权声明:本文为博主原创 ...

  2. php对excel导入导出的支持

    闲话不多说了,大家直接进入主题 php对excel的导入:  1.为程序添加一个form表单,为form标签添加“enctype="multipart/form-data"”属性 ...

  3. textarea统计字数

    开发项目中经常会用到,textarea统计字数 源码如下: <!DOCTYPE html><html lang="en"><head> < ...

  4. [codevs3657]括号序列

    题目大意:有一列只有'(',')','[',']'构成的括号序列,求在序列中至少加上多少括号,能使该序列合法. 解题思路:区间dp. 我们以$f[i][j]$表示把区间$[i,j]$添成合法括号所需的 ...

  5. /etc/rc.d/rc.sysinit

    [root@web02 ~]# ls /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit [root@web02 ~]# [root@web02 ~]# ls /etc ...

  6. thinkphp 5.0整合phpsocketio完整攻略,绕坑

    使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...

  7. vue项目打包后想发布在apache www/vue 目录下

    使用的是vue-element-admin做示例,可以参考Vue项目根据不同运行环境打包项目,其他项目应该大同小异. 使用vue-router的browserHistory模式,配置mode: 'hi ...

  8. 使用 vue + thinkjs 开发博客程序记录

    一入冬懒癌发作,给自己找点事干.之前博客程序写过几次,php 的写过两次,nodejs 用 ThinkJS 写过,随着 ThinkJS 版本从1.x 升级到 2.x 之前的博客程序也做过升级.但是因为 ...

  9. (50)与magento集成

    我对接的是 odoo8 和 magento1.9.x 准备工作: l  服务器 装上mangento 组件 : $  pip install magento 装上 requests 组件:$ pip ...

  10. 【转】Geometry cannot have Z values

    http://blog.csdn.net/tweeenty/article/details/44246407 在对矢量要素类添加要素,进行赋几何信息时(FeatureBuffer.Shape = IG ...