bzoj3171: [Tjoi2013] Cyclic grid

***** I really want to be rude but be civilized

I really flipped the car

I see this question, isn't this a cost flow?

Then I feel that the strong connection directly records the in-degree and out-degree, will it be OK soon, and then finish the code with confidence 1WA

Coming back to change the cost flow and building a map is annoying, and the result is that n is marked as m. . . waste of time without gain

The method is all right. Remove the points and then connect the edges to the surrounding points. The cost of the starting direction is 0, and the other is 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);
                for [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 = for [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;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891455&siteId=291194637