1. 程式人生 > >解題:2017清華集訓 無限之環

解題: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