CodeForces 316c1 Tidying Up
Tidying Up
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
2 3
1 1 2
2 3 3
2
3 4
1 3 2 6
2 1 5 6
4 4 5 3
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的更多相关文章
- Codeforces 最大流 费用流
这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- HCF4094(CD4094)应用
管脚说明和内部逻辑图 注:管脚图为HCF4094,内部逻辑图为CD4094(HCF4094内部逻辑图在datasheet不清晰,且复杂). 其中控制管脚有3个:STROBE-DATA-CLOCK,Ou ...
- SpringCloud学习笔记(3)----Spring Cloud Netflix之深入理解Eureka
1. Eureka服务端的启动过程 1.1 入口类EurekaServerInitializerConfiguration类, public void start() { (new Thread(n ...
- AJAX和JSON实际应用
实现功能:登录验证 一.因为我是在SpringMVC框架上写的,首先得添加依赖: <dependencies> <!-- 用来测试的依赖 --> <dependency& ...
- Url 简单讲解
eg: http://sb.test.com/login?name=liming&password=twotigers 协议 http https ftp 域名 sb.test.com 则是域 ...
- [agc015c]nuske vs phantom thnook
题意: 有一个n*m的网格图,每个格子是蓝色或白色.四相邻的两个格子连一条边,保证蓝格子构成一个森林. 有q组询问,每次询问给出一个矩形,问矩形内蓝格子组成的联通块个数. $1\leq n,m\leq ...
- K-D树学习笔记
这东西其实就是高维二叉树?(反正我只会二维的) 大概就是把一个高维矩形按每一维分,一个点(及其子树)就表示一个高维区间,乱搞一下,就……没了? //BZOJ4066 "简单"题 / ...
- BZOJ 3790 神奇项链(回文自动机+线段树优化DP)
我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...
- luogu P4430 小猴打架(prufer编码与Cayley定理)
题意 n个点问有多少种有顺序的连接方法把这些点连成一棵树. (n<=106) 题解 了解有关prufer编码与Cayley定理的知识. 可知带标号的无根树有nn-2种.然后n-1条边有(n-1) ...
- oracle和mysql的分页
如果我们是通过JDBC的方式访问数据库,那么就有必要根据数据库类型采取不同的SQL分页语句,对于MySql数据库,我们可以采用limit语句进行分页,对于Oracle数据库,我们可以采用rownum的 ...
- 紫书 习题 11-2 UVa 1001 (Floyd)
这道题只是在边上做一些文章. 这道题起点终点可以看成半径为0的洞, 我是直接加入了洞的数组. 边就是两点间的距离减去半径, 如果结果小于0的话, 距离就为0, 距离不能为负 然后我看到n只有100, ...