zoukankan      html  css  js  c++  java
  • 解题:2017清华集训 无限之环

    题面

    费用流

    把每种水管再拆出来四个方向的接头,然后根据水管的形状连出旋转时的代价。最后黑白染色成二分图,然后白点对应的接头向黑点对应的接头连边,源点向白点自己连边,黑点自己向汇点连边。

    怎么连边?我是大力讨论16种情况连的,应该是可以巧妙一点的=。=

    洛谷不开O2会T,LOJ过了

      1 // luogu-judger-enable-o2
      2 #include<queue>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<algorithm>
      6 using namespace std;
      7 const int B=2005,N=10005,M=120005,inf=1e9;
      8 const int mov[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
      9 int n,m,s,t,t1,t2,t3,t4,id,cnt,tot,sum,maxf,minc;
     10 int noww[2*M],goal[2*M],flow[2*M],cost[2*M];
     11 int p[N],mflw[N],mcst[N],queu[N],pren[N],pree[N];
     12 int mapp[B][B],idx[B][B],hed[B][B],tmp[5],hmap[5];
     13 queue<int> qs;
     14 void link(int f,int t,int v,int c,int x)
     15 {
     16     if(x) swap(f,t);
     17     noww[++cnt]=p[f],p[f]=cnt;
     18     goal[cnt]=t,flow[cnt]=v,cost[cnt]=c;
     19     noww[++cnt]=p[t],p[t]=cnt;
     20     goal[cnt]=f,flow[cnt]=0,cost[cnt]=-c;
     21 }
     22 int Bitcount(int x)
     23 {
     24     int ret=0;
     25     while(x)
     26         ret++,x-=x&-x;
     27     return ret; 
     28 }
     29 void Create(int x,int y,int z)
     30 {
     31     int typ=mapp[x][y],nde=idx[x][y];
     32     for(int i=1;i<=4;i++) tmp[i]=++tot;
     33     hed[x][y]=Bitcount(typ);
     34     if(!typ) 
     35         for(int i=1;i<=4;i++)
     36             link(nde,tmp[i],0,inf,z);
     37     else if(typ==1)
     38     {
     39         link(nde,tmp[1],1,0,z);
     40         link(tmp[1],tmp[2],1,1,z);
     41         link(tmp[1],tmp[3],1,2,z);
     42         link(tmp[1],tmp[4],1,1,z);
     43     } 
     44     else if(typ==2)
     45     {
     46         link(tmp[2],tmp[1],1,1,z);
     47         link(nde,tmp[2],1,0,z);
     48         link(tmp[2],tmp[3],1,1,z);
     49         link(tmp[2],tmp[4],1,2,z);
     50     } 
     51     else if(typ==3)
     52     {
     53         link(nde,tmp[1],1,0,z);
     54         link(nde,tmp[2],1,0,z);
     55         link(tmp[1],tmp[3],1,1,z);
     56         link(tmp[2],tmp[4],1,1,z);
     57     }
     58     else if(typ==4)
     59     {
     60         link(tmp[3],tmp[1],1,2,z);
     61         link(tmp[3],tmp[2],1,1,z);
     62         link(nde,tmp[3],1,0,z);
     63         link(tmp[3],tmp[4],1,1,z);
     64     }
     65     else if(typ==5)
     66         for(int i=1;i<=4;i++)
     67             link(nde,tmp[i],(i%2)?1:0,(i%2)?0:inf,z);
     68     else if(typ==6)
     69     {
     70         link(nde,tmp[2],1,0,z);
     71         link(nde,tmp[3],1,0,z);
     72         link(tmp[2],tmp[4],1,1,z);
     73         link(tmp[3],tmp[1],1,1,z);
     74     }
     75     else if(typ==7)
     76     {
     77         link(nde,tmp[1],1,0,z);
     78         link(nde,tmp[2],1,0,z);
     79         link(nde,tmp[3],1,0,z);
     80         link(tmp[1],tmp[4],1,1,z);
     81         link(tmp[2],tmp[4],1,2,z);
     82         link(tmp[3],tmp[4],1,1,z);
     83     }
     84     else if(typ==8)
     85     {
     86         link(tmp[4],tmp[1],1,1,z);
     87         link(tmp[4],tmp[2],1,2,z);
     88         link(tmp[4],tmp[3],1,1,z);
     89         link(nde,tmp[4],1,0,z);
     90     }
     91     else if(typ==9)
     92     {
     93         link(nde,tmp[1],1,0,z);
     94         link(nde,tmp[4],1,0,z);
     95         link(tmp[1],tmp[3],1,1,z);
     96         link(tmp[4],tmp[2],1,1,z);
     97     }
     98     else if(typ==10)
     99         for(int i=1;i<=4;i++)
    100             link(nde,tmp[i],(i%2)?0:1,(i%2)?inf:0,z);
    101     else if(typ==11)
    102     {
    103         link(nde,tmp[1],1,0,z);
    104         link(nde,tmp[2],1,0,z);
    105         link(nde,tmp[4],1,0,z);
    106         link(tmp[1],tmp[3],1,2,z);
    107         link(tmp[2],tmp[3],1,1,z);
    108         link(tmp[4],tmp[3],1,1,z);
    109     }
    110     else if(typ==12)
    111     {
    112         link(nde,tmp[3],1,0,z);
    113         link(nde,tmp[4],1,0,z);
    114         link(tmp[3],tmp[1],1,1,z);
    115         link(tmp[4],tmp[2],1,1,z);
    116     }
    117     else if(typ==13)
    118     {
    119         link(nde,tmp[1],1,0,z);
    120         link(nde,tmp[3],1,0,z);
    121         link(nde,tmp[4],1,0,z);
    122         link(tmp[1],tmp[2],1,1,z);
    123         link(tmp[3],tmp[2],1,1,z);
    124         link(tmp[4],tmp[2],1,2,z);
    125     }
    126     else if(typ==14)
    127     {
    128         link(nde,tmp[2],1,0,z);
    129         link(nde,tmp[3],1,0,z);
    130         link(nde,tmp[4],1,0,z);
    131         link(tmp[2],tmp[1],1,1,z);
    132         link(tmp[3],tmp[1],1,2,z);
    133         link(tmp[4],tmp[1],1,1,z);
    134     }
    135     else if(typ==15)
    136         for(int i=1;i<=4;i++)
    137             link(nde,tmp[i],1,0,z);
    138 }
    139 void Init(int st,int ed)
    140 {
    141     memset(mflw,0x3f,sizeof mflw);
    142     memset(mcst,0x3f,sizeof mcst);
    143     memset(queu,0,sizeof queu),pren[ed]=-1;
    144     qs.push(st),queu[st]=true,mcst[st]=0;
    145 }
    146 bool SP(int st,int ed)
    147 {
    148     Init(st,ed); 
    149     while(!qs.empty())
    150     {
    151         int tn=qs.front();
    152         qs.pop(); queu[tn]=false;
    153         for(int i=p[tn],g;i;i=noww[i])
    154             if(mcst[g=goal[i]]>mcst[tn]+cost[i]&&flow[i])
    155             {
    156                 pren[g]=tn,pree[g]=i;
    157                 mcst[g]=mcst[tn]+cost[i];
    158                 mflw[g]=min(mflw[tn],flow[i]);
    159                 if(!queu[g]) qs.push(g),queu[g]=true;
    160             }
    161     }
    162     return ~pren[ed];
    163 }
    164 void MCMF(int st,int ed)
    165 {
    166     while(SP(st,ed))
    167     {
    168         maxf+=mflw[ed],id=ed;
    169         minc+=mflw[ed]*mcst[ed]; 
    170         while(id!=st)
    171         {
    172             flow[pree[id]]-=mflw[ed];
    173             flow[pree[id]^1]+=mflw[ed];
    174             id=pren[id];
    175         }
    176     }
    177 }
    178 int main ()
    179 {
    180     scanf("%d%d",&n,&m),cnt=1;
    181     for(int i=1;i<=n;i++)
    182         for(int j=1;j<=m;j++)
    183         {
    184             scanf("%d",&mapp[i][j]);
    185             idx[i][j]=++tot,Create(i,j,(i+j)%2);
    186         }
    187     s=++tot,t=++tot;
    188     hmap[0]=3,hmap[1]=4,hmap[2]=1,hmap[3]=2;
    189     for(int i=1;i<=n;i++)
    190         for(int j=1;j<=m;j++)
    191         {
    192             if((i+j)%2==0) 
    193             {
    194                 link(s,idx[i][j],inf,0,0),sum+=hed[i][j];
    195                 for(int k=0;k<4;k++)
    196                 {
    197                     int tx=i+mov[k][0],ty=j+mov[k][1];
    198                     if(tx>=1&&tx<=n&&ty>=1&&ty<=m)
    199                         link(idx[i][j]+k+1,idx[tx][ty]+hmap[k],1,0,0);
    200                 }
    201             }
    202             else 
    203                 link(idx[i][j],t,inf,0,0);
    204         }
    205     MCMF(s,t);
    206     if(maxf!=sum) printf("-1");
    207     else printf("%d",minc);
    208     return 0;
    209 }
    View Code
  • 相关阅读:
    测试爬虫
    流式大数据处理的三种框架:Storm,Spark和Samza
    redo日志
    HTTP协议之chunk编码(分块传输编码
    QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)
    How to use kingshard building a MySQL cluster
    转载: Qt 学习之路 2归档
    Python与机器人技术
    Nginx配置正向代理
    使用Chrony配置 NTP
  • 原文地址:https://www.cnblogs.com/ydnhaha/p/10127523.html
Copyright © 2011-2022 走看看