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. Codeforces 982 C. Cut 'em all!(dfs)

    解题思路: 代码中有详细注解,以任意一点为根,dfs遍历这棵树. 每一个节点可能有好几个子树,计算每棵子树含有的节点数,再+1即为这整棵树的节点. 判断子树是否能切断与根之间的联系,如果子树含有偶数个 ...

  2. DataView和DefaultView的理解

    DefaultView 的作用(对DataSet查询出的来数据进行排序)(这个老哥的太长了) DefaultView:默认视图.datatable.DefaultView获取整个表的视图(就像sql查 ...

  3. Generating SSH Keys for github

    由于最近电脑重装了Windows 8.1, 想用github维护一些代码.故不得不重新生成一下ssh key. 按https://help.github.com/articles/generating ...

  4. 「JavaSE 重新出发」02. 数据类型与运算符

    「TOC」 Java 程序基本要求 Java 数据类型 基本数据类型 复合数据类型 运算符 逻辑运算符 位运算符 运算符优先级 Java 程序基本要求 public class : 一个 Java 文 ...

  5. 服务器搭建域控与SQL Server的AlwaysOn环境过程(三)配置故障转移

    0 引言 主要讲述如何搭建故障转移集群,因为AlwaysOn是基于Windows的故障转移集群的. 在讲解步骤之前需要了解一下故障转移集群仲裁配置 下面图片来自<Windows Server20 ...

  6. mybatis中 #跟$的区别

    #相当于对数据 加上 双引号,$相当于直接显示数据 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sq ...

  7. linux+apache+mysql+php平台构建及环境配置

    1.我使用的centos6.安装时已经选择安装apach.mysql,事实上在运行下列两行命令的时候又对其进行了更新.所以说装的时候能够不安装,免得浪费时间. yum install php-mysq ...

  8. 迷宫求解_数据结构c语言版

    #include <iostream> #include <string> #include <cstdio> #include <cstdlib> # ...

  9. lucene LZ4 会将doc存储在一个chunk里进行Lz4压缩 ES的_source便如此

    默认情况下,Elasticsearch 用 JSON 字符串来表示文档主体保存在 _source 字段中.像其他保存的字段一样,_source 字段也会在写入硬盘前压缩.The _source is ...

  10. 使用OpenCV把二进制mnist数据集转换为图片

    mnist数据集是以二进制形式保存的,这里借助OpenCV把mnist数据集转换成图片格式.转换程序如下: #include <iostream> #include <fstream ...