Exchange pieces

  Now in addition to upper and lower bounds and minimum cut network flow model (maximum weight subgraph closed) it does not prove the correctness of a class it will not use. The rest of the more skilled.

Good question this is my selection of a few feel like a long time to no avail look at the subject of the solution to a problem.

Questions asked around us can swap adjacent pieces, but also in the final exchange to limit the number of a final state.

1. If the state of the two pieces of black and white does not control the number of pieces it must be -1

2. This question is not difficult to find the minimum number of think-half but this dichotomy useless it does not have any relationship with our traffic only apparently discarded this idea has little contact with our process with the answers to the answer.

3. then be able to seek the minimum cost is obviously a cost flow of exchange we want to minimize the cost of the final cost of a maximum flow is the answer.

4. Note that this case is not exchanged but the number of pieces involved in the exchange of the lattice is limited.

The two pieces are exchanged obviously we just need to pursue the exchange of pieces gone out instead of being swapped out pieces gone we do not care. This is the focus, we must think of this thing for a flow starting from the source point we only care about where to go and what it will be exchanged ignore us directly.

Considering that the number of pieces to be exchanged must be different colors if the same color then the exchange will be meaningless is not it also increases the answers so be exchanged must be useless pieces.

6. We do not know swapped out if the exchange is still only a point to split into two points, then we can not distinguish this case.

7. split into three come from a point source because it is not a stream occupy a switching swapped out.

8. If the problem is split into a number of points and not point out a certain number of times more often than the incoming one point is not the point came out more than a point and a point bisecting it has nothing bisects it .

9 Even the cost of running costs can flow.

But in general it is such a correct code has been slightly modified but after 70 minutes I do not know why too can stay stone pit it help me God looked 2h really thank him.

//#include<bits/stdc++.h>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<deque>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<stack>
#include<algorithm>
#include<vector>
#include<cctype>
#include<utility>
#include<set>
#include<bitset>
#include<map>
#define INF 1000000000
#define ll long long
#define min(x,y) (x>y?y:x)
#define max(x,y) (x>y?x:y)
#define RI register ll
#define up(p,i,n) for(ll i=p;i<=n;++i)
#define db double
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int MAXN=25,maxn=MAXN*MAXN*MAXN*MAXN;
int n,m,S,T,cnt,sum,sum1,flag,len=1,t,h,maxflow,ans;
char c[MAXN][MAXN];
int a[MAXN][MAXN],pos[MAXN][MAXN],b[MAXN][MAXN];
int lin[maxn],nex[maxn],ver[maxn],e[maxn],e1[maxn];
int pre[maxn],in[maxn],vis[maxn],q[maxn],dis[maxn];
const int dx[9]={0,1,1,1,0,0,-1,-1,-1};
const int dy[9]={0,0,-1,1,1,-1,0,-1,1};
inline void add(int x,int y,int z,int z1)
{
    ver[++len]=y;nex[len]=lin[x];lin[x]=len;e[len]=z;e1[len]=z1;
    ver[++len]=x;nex[len]=lin[y];lin[y]=len;e[len]=0;e1[len]=-z1;
}
inline int spfa()
{
    for(int i=1;i<=T;++i)dis[i]=INF;
    t=h=0;q[++t]=S;vis[S]=1;dis[S]=0;in[S]=INF;
    while(h++<t)
    {
        int x=q[h];vis[x]=0;
        for(int i=lin[x];i;i=nex[i])
        {
            int tn=ver[i];
            if(!e[i])continue;
            if(dis[tn]>dis[x]+e1[i])
            {
                dis[tn]=dis[x]+e1[i];
                in[tn]=min(in[x],e[i]);
                pre[tn]=i;
                if(!vis[tn])q[++t]=tn,vis[tn]=1;
            }
        }
    }
    return dis[T]!=INF;
}
inline void EK()
{
    while(spfa())
    {
        maxflow+=in[T];
        ans+=in[T]*dis[T];
        int x=T,i=pre[x];
        while(x!=S)
        {
            e[i^1]+=in[T];
            e[i]-=in[T];
            x=ver[i^1];i=pre[x];
        }
    }
}
int main()
{
    //freopen("1.in","r",stdin);
    n=read();m=read();S=n*m*3+1;T=S+1;
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            pos[i][j]=++cnt;
            if(c[i][j]=='1')++sum,add(S,pos[i][j],1,0),b[i][j]=1;
        }
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(c[i][j]=='1')++sum1,add(pos[i][j],T,1,0),a[i][j]=1;
    if(sum!=sum1){printf("%d\n",-1);return 0;}
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            int w=(int)(c[i][j]-'0');
            for(int k=1;k<=8;++k)
            {
                int xx=i+dx[k];
                int yy=j+dy[k];
                if(!pos[xx][yy])continue;
                add(pos[i][j]+n*m*2,pos[xx][yy]+n*m,INF,1);
            }
            if((!(a[i][j]^b[i][j]))||(!(w&1)))
            {
                add(pos[i][j]+n*m,pos[i][j],(w>>1),0);
                add(pos[i][j],pos[i][j]+n*m*2,(w>>1),0);
            }
            else
            {
                add(pos[i][j]+n*m,pos[i][j],(w>>1)+a[i][j]?1:0,0);
                add(pos[i][j],pos[i][j]+n*m*2,(w>>1)+b[i][j]?1:0,0);
            }
        }
    EK();
    if(maxflow!=sum)printf("%d\n",-1);
    else printf("%d\n",ans);
    return 0;
}
70
//#include<bits/stdc++.h>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<deque>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<stack>
#include<algorithm>
#include<vector>
#include<cctype>
#include<utility>
#include<set>
#include<bitset>
#include<map>
#define INF 1000000000
#define ll long long
#define min(x,y) (x>y?y:x)
#define max(x,y) (x>y?x:y)
#define RI register ll
#define up(p,i,n) for(ll i=p;i<=n;++i)
#define db double
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int MAXN=25,maxn=MAXN*MAXN*24;
int n,m,S,T,cnt,sum,sum1,flag,len=1,t,h,maxflow,ans;
char c[MAXN][MAXN];
int a[MAXN][MAXN],pos[MAXN][MAXN],b[MAXN][MAXN];
int lin[maxn],nex[maxn],ver[maxn],e[maxn],e1[maxn];
int pre[maxn],in[maxn],vis[maxn],q[maxn],dis[maxn];
const int dx[9]={0,1,1,1,0,0,-1,-1,-1};
const int dy[9]={0,0,-1,1,1,-1,0,-1,1};
inline void add(int x,int y,int z,int z1)
{
    ver[++len]=y;nex[len]=lin[x];lin[x]=len;e[len]=z;e1[len]=z1;
    ver[++len]=x;nex[len]=lin[y];lin[y]=len;e[len]=0;e1[len]=-z1;
}
inline int spfa()
{
    for(int i=1;i<=T;++i)dis[i]=INF;
    t=h=0;q[++t]=S;vis[S]=1;dis[S]=0;in[S]=INF;
    while(h++<t)
    {
        int x=q[h];vis[x]=0;
        for(int i=lin[x];i;i=nex[i])
        {
            int tn=ver[i];
            if(!e[i])continue;
            if(dis[tn]>dis[x]+e1[i])
            {
                dis[tn]=dis[x]+e1[i];
                in[tn]=min(in[x],e[i]);
                pre[tn]=i;
                if(!vis[tn])q[++t]=tn,vis[tn]=1;
            }
        }
    }
    return dis[T]!=INF;
}
inline void EK()
{
    while(spfa())
    {
        maxflow+=in[T];
        ans+=in[T]*dis[T];
        int x=T,i=pre[x];
        while(x!=S)
        {
            e[i^1]+=in[T];
            e[i]-=in[T];
            x=ver[i^1];i=pre[x];
        }
    }
}
int main()
{
    //freopen("1.in","r",stdin);
    n=read();m=read();S=n*m*3+1;T=S+1;
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            pos[i][j]=++cnt;
            if(c[i][j]=='1')++sum,add(S,pos[i][j],1,0),b[i][j]=1;
        }
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(c[i][j]=='1')++sum1,add(pos[i][j],T,1,0),a[i][j]=1;
    if(sum!=sum1){printf("%d\n",-1);return 0;}
    for(int i=1;i<=n;++i)scanf("%s",c[i]+1);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            int w=(int)(c[i][j]-'0');
            for(int k=1;k<=8;++k)
            {
                int xx=i+dx[k];
                int yy=j+dy[k];
                if(!pos[xx][yy])continue;
                add(pos[i][j]+n*m*2,pos[xx][yy]+n*m,INF,1);
            }
            if((!(a[i][j]^b[i][j]))||(!(w&1)))
            {
                add(pos[i][j]+n*m,pos[i][j],w>>1,0);
                add(pos[i][j],pos[i][j]+n*m*2,w>>1,0);
            }
            else
            {
                --w;
                add(pos[i][j]+n*m,pos[i][j],(w>>1)+a[i][j],0);
                add(pos[i][j],pos[i][j]+n*m*2,(w>>1)+b[i][j],0);
            }
        }
    EK();
    if(maxflow!=sum)printf("%d\n",-1);
    else printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/chdy/p/11104903.html
Recommended