codeforces 316C Tidying Up(最佳完美匹配)

 

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.

 

题目大意:在n*m的矩阵上面,每个格子里面有一个数字x(1≤x≤n*m/2),每个数字出现2次,现需要让所有相同的数字相邻,问最少需要有多少个格子的数要改变位置。
思路:建图,每个数字与旁边4格都连一条边,若相同则边权为0,不相同则边权为1,最佳完美匹配为答案。因为若不同的数字匹配,那么两个数字必然要有一个换位置,而在某个匹配中整个图中换得最少的方案可以连成一个或多个环,交换的次数恰好是最佳完美匹配。

下面采用据说在二分图中速度很快的ZKW费用流做法。邻接表版的KM算法应该也可以做。

至于为什么这么大的复杂度也不会超时……估计CF的机子真太好了……

  1 #include <cstdio>

  2 #include <queue>

  3 #include <utility>

  4 #include <cstring>

  5 using namespace std;

  6 

  7 #define MAXN 10010

  8 #define MAXM 100010

  9 #define INF 0x3f3f3f3f

 10 

 11 struct ZKW_flow{

 12     int S, T, ecnt;

 13     int head[MAXN];

 14     int c[MAXM], w[MAXM], to[MAXM], next[MAXM];

 15     ZKW_flow(){

 16         memset(head,0,sizeof(head));

 17         ecnt = 2;

 18     }

 19 

 20     void addEdge(int u, int v, int _c, int ww){

 21         c[ecnt] = _c; w[ecnt] = ww; to[ecnt] = v;

 22         next[ecnt] = head[u]; head[u] = ecnt++;

 23         c[ecnt] = 0; w[ecnt] = -ww; to[ecnt] = u;

 24         next[ecnt] = head[v]; head[v] = ecnt++;

 25     }

 26 

 27     int dis[MAXN];

 28 

 29     void SPFA(){

 30         for(int i = 1; i <= T; ++i) dis[i] = INF;

 31         priority_queue<pair<int, int> > Q;

 32         dis[S] = 0;

 33         Q.push(make_pair(0, S));

 34         while(!Q.empty()){

 35             int u = Q.top().second, d = -Q.top().first;

 36             Q.pop();

 37             if(dis[u] != d) continue;

 38             for(int p = head[u]; p; p = next[p]){

 39                 int &v = to[p];

 40                 if(c[p] && dis[v] > d + w[p]){

 41                     dis[v] = d + w[p];

 42                     Q.push(make_pair(-dis[v], v));

 43                 }

 44             }

 45         }

 46         for(int i = 1; i <= T; ++i) dis[i] = dis[T] - dis[i];

 47     }

 48 

 49     int ans;

 50     bool use[MAXN];

 51 

 52     int add_flow(int u, int flow){

 53         if(u == T){

 54             ans += dis[S] * flow;

 55             return flow;

 56         }

 57         use[u] = true;

 58         int now = flow;

 59         for(int p = head[u]; p; p = next[p]){

 60             int &v = to[p];

 61             if(c[p] && !use[v] && dis[u] == dis[v] + w[p]){

 62                 int tmp = add_flow(v, min(now, c[p]));

 63                 c[p] -= tmp;

 64                 c[p^1] += tmp;

 65                 now -= tmp;

 66                 if(!now) break;

 67             }

 68         }

 69         return flow - now;

 70     }

 71 

 72     bool modify_label(){

 73         int d = INF;

 74         for(int u = 1; u <= T; ++u) if(use[u])

 75             for(int p = head[u]; p; p = next[p]){

 76                 int &v = to[p];

 77                 if(c[p] && !use[v]) d = min(d, dis[v] + w[p] - dis[u]);

 78             }

 79         if(d == INF) return false;

 80         for(int i = 1; i <= T; ++i) if(use[i]) dis[i] += d;

 81         return true;

 82     }

 83 

 84     int min_cost_flow(){

 85         SPFA();

 86         while(true){

 87             while(true){

 88                 for(int i = 1; i <= T; ++i) use[i] = 0;

 89                 if(!add_flow(S, INF)) break;

 90             }

 91             if(!modify_label()) break;

 92         }

 93         return ans;

 94     }

 95 } G;

 96 

 97 int n,m;

 98 int mp[110][110];

 99 

100 inline int pos(int i, int j){

101     return (i-1)*m + j;

102 }

103 

104 int main(){

105     scanf("%d%d", &n, &m);

106     for(int i = 1; i <= n; ++i)

107         for(int j = 1; j <= m; ++j) scanf("%d", &mp[i][j]);

108     G.S = n*m+1; G.T = G.S+1;

109     for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j)

110         if((i+j)&1){

111             G.addEdge(G.S, pos(i,j), 1, 0);

112             if(i-1>=1) G.addEdge(pos(i,j),pos(i-1,j),1,mp[i][j]!=mp[i-1][j]);

113             if(i+1<=n) G.addEdge(pos(i,j),pos(i+1,j),1,mp[i][j]!=mp[i+1][j]);

114             if(j-1>=1) G.addEdge(pos(i,j),pos(i,j-1),1,mp[i][j]!=mp[i][j-1]);

115             if(j+1<=m) G.addEdge(pos(i,j),pos(i,j+1),1,mp[i][j]!=mp[i][j+1]);

116         }

117         else G.addEdge(pos(i,j), G.T, 1, 0);

118     printf("%d\n", G.min_cost_flow());

119     return 0;

120 }
15ms

 

你可能感兴趣的:(codeforces)