zoukankan      html  css  js  c++  java
  • CF 316C2(Tidying Up-二分图最大边权)

    C2. Tidying Up
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    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
    
    Note

     

    The second sample.
    转换为2分图最大边权
    费用流。
    建图太渣。


    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    #include<cmath>
    #include<cctype>
    using namespace std;
    #define For(i,n) for(int i=1;i<=n;i++)
    #define Rep(i,n) for(int i=0;i<n;i++)
    #define Fork(i,k,n) for(int i=k;i<=n;i++)
    #define ForD(i,n) for(int i=n;i;i--)
    #define Forp(x) for(int p=pre[x];p;p=next[p])
    #define RepD(i,n) for(int i=n;i>=0;i--)
    #define MAXN (80*80+10)
    #define MAXL (80+10)
    #define MAXM (MAXL*MAXL*4+MAXN*4)
    #define INF (2139062143)
    int q[MAXN*8],d[MAXN],pr[MAXN],ed[MAXN],b[MAXN]; 
    int edge[MAXM],next[MAXM],cost[MAXM],weight[MAXM],pre[MAXN],size=1;
    void addedge(int u,int v,int w,int c)
    {
    	edge[++size]=v;
    	weight[size]=w;
    	cost[size]=c;
    	next[size]=pre[u];
    	pre[u]=size;
    }
    void addedge2(int u,int v,int w,int c){addedge(u,v,w,c),addedge(v,u,0,-c);}
    bool SPFA(int s,int t)
    {
    	memset(d,127,sizeof(d));
    	memset(b,0,sizeof(b));
    	d[q[1]=s]=0;b[s]=1;
    	int head=1,tail=1;
    	while (head<=tail)
    	{
    		int now=q[head++];
    		Forp(now)
    		{
    			int &v=edge[p];
    			if (weight[p]&&d[now]+cost[p]<d[v])
    			{
    				d[v]=d[now]+cost[p];
    				if (!b[v]) b[v]=1,q[++tail]=v;
    				pr[v]=now,ed[v]=p;
    			}
    		}
    		b[now]=0;
    	}
    	return d[t]<d[0];
    }
    int CostFlow(int s,int t)
    {
    	int totcost=0;
    	while (SPFA(s,t))
    	{
    		int flow=INF;
    		for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]); 	
    		totcost+=flow*d[t];
    		for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow; 	
    	}
    	return totcost;
    }
    int n,m,a[MAXL][MAXL];
    int no(int i,int j){return (i-1)*m+j;}
    int main()
    {
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	cin>>n>>m;
    	int s=n*m+1,t=n*m+2;
    	For(i,n) For(j,m) cin>>a[i][j];
    	For(i,n) For(j,m) 
    	{
    		if (i+j&1) addedge2(s,no(i,j),1,0);
    		else addedge2(no(i,j),t,1,0);
    		if (j<m)
    		{
    			if (i+j&1) addedge2(no(i,j),no(i,j+1),1,a[i][j]!=a[i][j+1]);
    			else addedge2(no(i,j+1),no(i,j),1,a[i][j]!=a[i][j+1]);
    		}
    		if (i<n)
    		{
    			if (i+j&1) addedge2(no(i,j),no(i+1,j),1,a[i][j]!=a[i+1][j]);
    			else addedge2(no(i+1,j),no(i,j),1,a[i][j]!=a[i+1][j]);
    		}		
    	}
    	cout<<CostFlow(s,t)<<endl;
    	return 0;
    }




     

  • 相关阅读:
    2015-05-27 用正则把oracle时间转化到mysql时间
    linux版idea14界面美观和windows,mac基本一致
    ubuntu 下自定义快捷键,,用着舒服
    ubuntu 方便好用的截图软件
    Integer 包装器类 大小比较
    win7、ubuntu双系统,遇到分区不可用问题,和卸载ubuntu后win7开不了机
    巧妙小思想
    读取16进制文件和校验图片格式的问题。 文件名后缀
    旧电脑变废为宝!
    Win10打开Autodesk软件时提示“管理员已阻止你运行此应用”
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3144760.html
Copyright © 2011-2022 走看看