zoukankan      html  css  js  c++  java
  • bzoj 3171: [Tjoi2013]循环格

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #define M 10000
      5 #define inf 2139062143
      6 using namespace std;
      7 int cnt=1,n,m,ans,T,d[M],q[2*M],f[M],head[M],next[10*M],u[10*M],v[10*M],w[10*M],fro[10*M],fr[M];
      8 int mp[100][100],xx[4]={0,0,1,-1},yy[4]={-1,1,0,0};
      9 void jia1(int a1,int a2,int a3,int a4)
     10 {
     11     cnt++;
     12     next[cnt]=head[a1];
     13     head[a1]=cnt;
     14     fro[cnt]=a1;
     15     u[cnt]=a2;
     16     v[cnt]=a3;
     17     w[cnt]=a4;
     18 }
     19 void jia(int a1,int a2,int a3,int a4)
     20 {
     21     jia1(a1,a2,a3,a4);
     22     jia1(a2,a1,0,-a4);
     23     return;
     24 }
     25 bool spfa()
     26 {
     27     memset(d,127,sizeof(int)*(T+1));
     28     d[0]=0;
     29     f[0]=1;
     30     q[1]=0;
     31     int h=0,t=1;
     32     for(;h<t;)
     33       {
     34         h++;
     35         int p=q[h];
     36         f[p]=0;
     37           for(int i=head[p];i;i=next[i])
     38           if(v[i]&&d[u[i]]>d[p]+w[i])
     39             {
     40                 d[u[i]]=d[p]+w[i];
     41                 fr[u[i]]=i;
     42                 if(!f[u[i]])
     43                   {
     44                     f[u[i]]=1;
     45                     t++;
     46                     q[t]=u[i];
     47                     }
     48             }
     49       }
     50     if(d[T]!=inf)
     51       return 1;
     52     return 0;
     53 }
     54 void mcf()
     55 {
     56     int mx=inf;
     57     for(int i=fr[T];i;i=fr[fro[i]])
     58       mx=min(mx,v[i]);
     59     for(int i=fr[T];i;i=fr[fro[i]])
     60       {
     61         v[i]-=mx;
     62         v[i^1]+=mx;
     63         ans+=mx*w[i];
     64       }
     65     return;
     66 }
     67 int main()
     68 {
     69     char ch[20];
     70     scanf("%d%d",&n,&m);
     71     T=n*m*2+1;
     72     for(int i=1;i<=n;i++)
     73       {
     74         scanf("%s",ch+1);
     75         for(int j=1;j<=m;j++)
     76           {
     77             if(ch[j]=='L')
     78               mp[i][j]=0;
     79             if(ch[j]=='R')
     80               mp[i][j]=1;
     81             if(ch[j]=='D')
     82               mp[i][j]=2;
     83             if(ch[j]=='U')
     84               mp[i][j]=3;
     85           }
     86       }
     87     for(int i=1;i<=n;i++)
     88       for(int j=1;j<=m;j++)
     89         {
     90             jia(0,(i-1)*m+j,1,0);
     91             jia((i-1)*m+n*m+j,T,1,0);
     92             for(int k=0;k<4;k++)
     93               {
     94                 int nx=i+xx[k],ny=j+yy[k]; 
     95                 if(!nx)
     96                   nx=n;
     97                 if(!ny)
     98                   ny=m;
     99                 if(nx>n)
    100                   nx=1;
    101                 if(ny>m)
    102                   ny=1;
    103                 if(k==mp[i][j])
    104                   jia((i-1)*m+j,(nx-1)*m+ny+n*m,1,0);
    105                 else
    106                   jia((i-1)*m+j,(nx-1)*m+ny+n*m,1,1);
    107               }
    108         }
    109     for(;spfa();)
    110       mcf();
    111     printf("%d
    ",ans);
    112     return 0;
    113 }

    循环格出入度都等于1,与一开始方向不同的加上费用,跑费用流,

  • 相关阅读:
    switch中局部变量的使用
    boost中regex的使用
    C++发送邮件jwsmpt
    c++编译相关
    一种处理多任务的线程池设计
    ICE的Timer和TimerTask
    bind2nd使用
    关于.NET WebSocket connection to 'ws://xxx' failed: Error during WebSocket handshake: Unexpected response code: 200
    QWrap Selector简介
    围观tangram
  • 原文地址:https://www.cnblogs.com/xydddd/p/5309124.html
Copyright © 2011-2022 走看看