bzoj3171: [Tjoi2013]循环格

*****我很想爆粗但是要文明好气噢

我是真的翻大车了

这题我一看这不是费用流吗zz

然后感觉强连通直接记一下出度入度不久行了吗,然后码完自信1WA

回来改费用流建图烦的要死,结果是n打成m。。。浪费时间没有收获

做法都会啊拆点然后向四周出点连边,开始方向费用0其他为1

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=999999999;

struct node
{
    int x,y,c,d,next,other;
}a[210000];int len,last[510];
void ins(int x,int y,int c,int d)
{
    int k1,k2;
    
    len++;k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
    a[len].next=last[x];last[x]=len;
    
    len++;k2=len;
    a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
    a[len].next=last[y];last[y]=len;
    
    a[k1].other=k2;
    a[k2].other=k1;
}

int ans,st,ed;
int list[510];bool v[510];
int d[510],pre[510],cc[510];
bool spfa()
{
    memset(d,63,sizeof(d));d[st]=0;
    memset(cc,0,sizeof(cc));cc[st]=inf;
    memset(v,false,sizeof(v));v[st]=true;
    int head=1,tail=2;list[1]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[y]>d[x]+a[k].d&&a[k].c>0)
            {
                d[y]=d[x]+a[k].d;
                cc[y]=min(cc[x],a[k].c);
                pre[y]=k;
                if(v[y]==false)
                {
                    v[y]=true;
                    list[tail]=y;
                    tail++;if(tail==505)tail=1;
                }
            }
        }
        v[x]=false;
        head++;if(head==505)head=1;
    }
    if(d[ed]>=inf)return false;
    else
    {
        ans+=cc[ed]*d[ed];
        int y=ed;
        while(pre[y]!=0)
        {
            int k=pre[y];
            a[k].c-=cc[ed];
            a[a[k].other].c+=cc[ed];
            y=a[k].x;
        }
        return true;
    }
}

int n,m;
int point(int x,int y){return (x-1)*m+y;}
int U[20][20],D[20][20],L[20][20],R[20][20];
char ss[20];
int main()
{
    scanf("%d%d",&n,&m);st=2*n*m+1,ed=2*n*m+2;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",ss+1);
        for(int j=1;j<=m;j++)
        {
            U[i][j]=1;D[i][j]=1;L[i][j]=1;R[i][j]=1;
                 if(ss[j]=='U')U[i][j]=0;
            else if(ss[j]=='D')D[i][j]=0;
            else if(ss[j]=='L')L[i][j]=0;
            else if(ss[j]=='R')R[i][j]=0;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(i==1)ins(point(i,j),point(n,j)+n*m,1,U[i][j]);
            else ins(point(i,j),point(i-1,j)+n*m,1,U[i][j]);
            
            if(i==n)ins(point(i,j),point(1,j)+n*m,1,D[i][j]);
            else ins(point(i,j),point(i+1,j)+n*m,1,D[i][j]);
            
            if(j==1)ins(point(i,j),point(i,m)+n*m,1,L[i][j]);
            else ins(point(i,j),point(i,j-1)+n*m,1,L[i][j]);
            
            if(j==m)ins(point(i,j),point(i,1)+n*m,1,R[i][j]);
            else ins(point(i,j),point(i,j+1)+n*m,1,R[i][j]);
            
            ins(st,point(i,j),1,0);
            ins(point(i,j)+n*m,ed,1,0);
        }
    ans=0;
    while(spfa()==true);
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2018-04-25 19:01  AKCqhzdy  阅读(102)  评论(0编辑  收藏  举报