bzoj2668: [cqoi2012]交换棋子

这题真是做死我了,调了一个早上。。
然后还没用弄出一个正确的图QAQ
于是下午时突发奇想,又想到了一些以为很对的东西,于是又搞错了
ORZ
感觉思路大概都对。。
注意时大概。。
但是还是,怎么说,思路还是有一点点混乱吧。。
总结一下问题:没有一个清晰的方向,就是有一个问题就补一个,于是就永远补不完了。。
看见这个问题,就拆点。。
那个问题又限制了流量。。
于是问题就一个接一个。。
╮(╯▽╰)╭
下次一定要理清思路再搞,不可以盲目地瞎搞。。
贴一个我的WA代码【费了很多心血的】

#include<cstdio>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
using namespace std;
const int N=25*25*4;
const int MAX=1<<30;
int n,m;
char ss[N];//起始状态    终止状态     可以移多少次
char k[N][N];
int st,ed;
int X[8]={-1,0,1,-1,1,-1,0,1};
int Y[8]={-1,-1,-1,0,0,1,1,1};
int cnt=0,cnt1=0;
struct qq
{
    int x,y,z,z1,last;//流量   费用 
}s[25*25*30*2];
bool ok[25][25];
bool ok1[25][25];
int num,last[N];
void init (int x,int y,int z,int z1)
{
    num++;
    s[num].x=x;s[num].y=y;s[num].z=z;s[num].z1=z1;
    s[num].last=last[x];
    last[x]=num;
    swap(x,y);z=0;z1=-z1;
    num++;
    s[num].x=x;s[num].y=y;s[num].z=z;s[num].z1=z1;
    s[num].last=last[x];
    last[x]=num;
}
int from[N];
int f[N];
bool in[N];
bool SPFA ()
{
    memset(in,false,sizeof(in));
    memset(from,-1,sizeof(from));
    memset(f,127,sizeof(f));
    queue<int> q;
    q.push(st);
    f[st]=0;in[st]=true;
    while (!q.empty())
    {
        int x=q.front();q.pop();
        //printf("%d ",x);
        for (int u=last[x];u!=-1;u=s[u].last)
        {
            int y=s[u].y;
            /*printf("NO:%d %d\n",y,u);
            system("pause");*/
            if (s[u].z>0&&f[y]>f[x]+s[u].z1)
            {
                f[y]=f[x]+s[u].z1;
                from[y]=u;
                if (in[y]==false)
                {
                    in[y]=true;
                    q.push(y);
                }
            }
        }
        in[x]=false;
    }
  //  printf("YES");
    return from[ed]!=-1;
}
int mymin (int x,int y)
{
    return x<y?x:y;
}
int ans=0;
void lalal ()
{
    int x=from[ed],t=MAX;
  //  printf("%d ",ed);
    while (x!=-1)
    {
        t=mymin(t,s[x].z);
     //   printf("%d ",s[x].x);
        x=from[s[x].x];
    }
 //   printf("\n");
    x=from[ed];
    while (x!=-1)
    {
        ans=ans+t*s[x].z1;
        s[x].z-=t;
        s[x^1].z+=t;
  //      printf("%d ",ans);
        x=from[s[x].x];
    }
  //  printf("\n");
    return ;
}
int P (int x,int y){return (x-1)*m+y;}
bool check ()//是不是满流
{
    for (int u=last[ed];u!=-1;u=s[u].last)
    {
    //  printf("YES:%d %d %d %d\n",s[u].x,s[u].y,s[u].z,s[u].z1);
        if (s[u].z<=0) return false;
    }
    return true;
}
int main()
{
    num=1;
    memset(ok,false,sizeof(ok));
    memset(ok1,false,sizeof(ok1));
    memset(last,-1,sizeof(last));
    scanf("%d%d",&n,&m);
    st=n*m*3+1;ed=st+1;
    for (int u=1;u<=n;u++)
        scanf("%s",k[u]+1);
    for (int u=1;u<=n;u++)
    {
        scanf("%s",ss+1);
        for (int i=1;i<=m;i++)
            if (ss[i]=='1')
            {
                if (k[u][i]=='1') {k[u][i]='0';continue;}
                ok[u][i]=true;
                cnt1++;
                init(P(u,i),ed,1,0);
            }
    }
    for (int u=1;u<=n;u++)
        for (int i=1;i<=m;i++)
            if (k[u][i]=='1')
            {
                cnt++;
                init(st,P(u,i),1,0);
            }
    if (cnt!=cnt1)  {printf("-1\n");return 0;}
    for (int u=1;u<=n;u++)
    {
        scanf("%s",ss+1);
        for (int i=1;i<=m;i++)
        {
            int x=ss[i]-'0';
            init(P(u,i)+n*m,P(u,i),(x+1)/2,0);
            init(P(u,i),P(u,i)+n*m*2,(x+1)/2,0);
            for (int j=0;j<8;j++)
            {
                int tx=u+X[j],ty=i+Y[j];
                if (tx<=0||tx>n||ty<=0||ty>m) continue;
                init(P(u,i)+n*m*2,P(tx,ty)+n*m,MAX,1);
            }
        }
    }
    while (SPFA()==true)    
    {
        lalal();
    }
    if (check()==true)  printf("%d\n",ans);
    else printf("-1");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值