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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 转:EL表达式
简介: EL 全名为 Language ,JSP2.0 之后,EL 成为了标准规范.因此,只要是支持Servlet2.4/JSP2.0 的容器,就都可以在JSP 网页中直接使用EL . 除了JSP2. ...
- 【转载】tom的RUNSTATS测试工具
-- 创建 runstats 包.其中包括 3 个简单 API 调用: create or replace package runstats_pkg as procedure rs_start; pr ...
- 待解决问题 oc
读书破万卷 Associated Object hash实现 Dynamic Method Resolution Message Forwarding forwardingTargetForSelec ...
- CSS3新增的属性有哪些:
CSS 用于控制网页的样式和布局. CSS3 是最新的 CSS 标准. CSS3新增了很多的属性,下面一起来分析一下新增的一些属性: 1.CSS3边框: border-radius:CSS3圆角边框. ...
- Windows 10用户可以快速移除U盘
新浪科技讯,北京时间 4 月 9 日上午消息,据美国科技媒体 The Verge 报道,微软再次证实,从 1809 版本的 Windows 10 开始,插入新闪存盘时“快速移除”(quick remo ...
- STM32是如何进入中断服务函数xxx_IRQHandler的
今天在看stm32的中断,一时间不理解stm32主函数是如何进入中断函数的,按C编程的理解,会有个特定的入口之类的,但是看demo过程中没有发现入口. 以串口中断服务函数void USART1_IRQ ...
- root用户删除文件提示:Operation not permitted
root用户删除文件提示:Operation not permitted http://blog.csdn.net/evanbai/article/details/6187578
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Java实现二叉树的创建、递归/非递归遍历
近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...
- nj04---事件回调函数
一.回调函数 1.异步式读取文件 var fs=require('fs'); fs.readFile('file.txt','utf-8',function(err,data){ if(err){ c ...