1 条题解
-
0
#include<cstdio> #include<cstring> using namespace std; struct node { int y,c,d,next; }a[210000];int n,m,k,len=1,cost,last[21000]; int st,ed; int zh(int x,int y,int p){return ((x-1)*m+y)*2-(p^1);} void ins(int x,int y,int c,int d) { len++; a[len].y=y;a[len].c=c;a[len].d=d;a[len].next=last[x];last[x]=len; len++; a[len].y=x;a[len].c=0;a[len].d=-d;a[len].next=last[y];last[y]=len; } int d[21000],list[21000],head,tail,cur[21000]; bool v[21000]; bool spfa() { memset(d,10,sizeof(d));d[ed]=0; v[ed]=false;head=1;tail=2;list[head]=ed; int inf=d[st]; while(head!=tail) { int x=list[head]; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(a[k^1].c>0 && d[x]-a[k].d<d[y]) { d[y]=d[x]-a[k].d; if(v[y]==true) { v[y]=false; if(d[y]<d[list[head+1]]) { int all=head; head--;if(head==0)head=ed+1; list[head]=list[all];list[all]=y; } else { list[tail++]=y;if(tail==ed+2)tail=1; } } } } head++;if(head==ed+2)head=1; v[x]=true; } return d[st]!=inf; } int mymin(int x,int y){return x<y?x:y;} int find(int x,int f) { if(x==ed)return f; v[x]=false; int ans=0,t,rule=cur[x]; for(int k=cur[x];k;k=a[k].next) { int y=a[k].y; if(a[k].c>0 && v[y]==true && d[y]==d[x]-a[k].d) { ans+=t=find(y,mymin(a[k].c,f-ans)); a[k].c-=t;a[k^1].c+=t;cost+=t*a[k].d;cur[x]=k; if(ans==f){v[x]=true;return ans;} } } for(int k=last[x];k!=rule;k=a[k].next) { int y=a[k].y; if(a[k].c>0 && v[y]==true && d[y]==d[x]-a[k].d) { ans+=t=find(y,mymin(a[k].c,f-ans)); a[k].c-=t;a[k^1].c+=t;cost+=t*a[k].d;cur[x]=k; if(ans==f){v[x]=true;return ans;} } } v[x]=true; return ans; } struct ansll { int x,y,z; }t_t1,t_t2; void nizhuan(int x,ansll &ttt) { ttt.z=(x%2)^1; x+=x%2;x/=2; ttt.y=x%m; ttt.x=x/m+1; if(ttt.y==0) { ttt.y=m;ttt.x--; } } int main() { scanf("%d%d%d",&k,&m,&n); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { int x;scanf("%d",&x); if(x!=1) { if(x==2)ins(zh(i,j,0),zh(i,j,1),1,-1); ins(zh(i,j,0),zh(i,j,1),999999999,0); } if(j!=m)ins(zh(i,j,1),zh(i,j+1,0),999999999,0); if(i!=n)ins(zh(i,j,1),zh(i+1,j,0),999999999,0); } } st=0;ed=n*m*2+1; for(int i=st;i<=ed;i++)cur[i]=last[i]; ins(st,zh(1,1,0),k,0);ins(zh(n,m,1),ed,k,0); int ans=0; memset(v,true,sizeof(v)); while(spfa())ans=find(st,999999999); for(int qq=1;qq<=k;qq++) { int stl=zh(1,1,1); while(stl!=zh(n,m,1)) { for(int kt=last[stl];kt;kt=a[kt].next) { int y=a[kt].y; if(y>stl && a[kt^1].c>0) { a[kt^1].c--; nizhuan(stl,t_t1); nizhuan(y,t_t2); if(t_t2.y==t_t1.y+1)printf("%d 1\n",qq); else printf("%d 0\n",qq); stl=y+1; break; } } } } return 0; }
- 1
信息
- ID
- 975
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 3
- 上传者