#include <stdio.h> int main() { puts("转载请注明出处"); puts("地址:blog.csdn.net/vmurder/article/details/43666807"); }
题解:
首先floyd或者bfs求出每个‘.‘到各个‘D‘的最短路
然后注意一个点不能经过一个门去另一个门,所以可以看到我的floyd略有一点点点点不同。。。
然后这个时间限制可以转化为对每个门的拆点,可以证明拆400个就够了。
然后分别对应每个时间可以出去一个人,
而这样在二分时重建图也是很方便的,
代码:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define P 25 #define N 500 #define M 300000 #define R 500 #define inf 0x3f3f3f3f using namespace std; const int dx[]={0,0,1,-1}; const int dy[]={1,-1,0,0}; struct KSD { int v,len,next; int flow; void init(){len=flow;} }e[M]; int head[50000],cnt; inline void add(int u,int v,int len) { e[++cnt].v=v; e[cnt].len=e[cnt].flow=len; e[cnt].next=head[u]; head[u]=cnt; } inline void Add(int u,int v,int len){add(u,v,len),add(v,u,0);} int s,t,d[50000]; queue<int>q; bool bfs() { memset(d,0,sizeof d); while(!q.empty())q.pop(); int i,u,v; q.push(s),d[s]=1; while(!q.empty()) { u=q.front(),q.pop(); for(i=head[u];i;i=e[i].next) { if(!d[v=e[i].v]&&e[i].len) { d[v]=d[u]+1; if(v==t)return 1; q.push(v); } } } return 0; } int dinic(int x,int flow) { if(x==t)return flow; int remain=flow,i,v,k; for(i=head[x];i&&remain;i=e[i].next) { if(e[i].len&&d[v=e[i].v]==d[x]+1) { k=dinic(v,min(remain,e[i].len)); if(!k)d[v]=0; e[i].len-=k,e[i^1].len+=k; remain-=k; } } return flow-remain; } int blank[N],blanks; int door[N],doors; int point[N][R+5],rec[N][R+5]; bool is_door[N]; void build(int mid) { int i,j; for(i=2;i<=cnt;i++)e[i].init(); for(i=1;i<=doors;i++)for(j=mid+1;j<=R;j++)e[rec[i][j]].len=0; return ; } int n,m,id[P][P]; int map[N][N]; char src[P][P]; void Build(int &l,int &r) { int i,j,k; int tx,ty; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%s",src[i]+1); for(i=1;i<=n;i++)for(j=1;j<=m;j++) { if(src[i][j]=='.')blank[++blanks]=id[i][j]=++cnt; else if(src[i][j]=='D') { door[++doors]=id[i][j]=++cnt; is_door[cnt]=true; } } memset(map,0x3f,sizeof map); for(i=1;i<=cnt;i++)map[i][i]=0; for(i=1;i<=n;i++)for(j=1;j<=m;j++)if(id[i][j]) for(k=0;k<4;k++)if(id[tx=i+dx[k]][ty=j+dy[k]]) map[id[i][j]][id[tx][ty]]=1; for(k=1;k<=cnt;k++) { if(is_door[k])continue; for(i=1;i<=cnt;i++)for(j=1;j<=cnt;j++)map[i][j]=min(map[i][j],map[i][k]+map[k][j]); } cnt=blanks; for(i=1;i<=doors;i++)for(j=1;j<=R;j++)point[i][j]=++cnt; s=0,t=cnt+1,cnt=1; for(i=1;i<=blanks;i++) { Add(s,i,1); // 500 int res=inf; for(j=1;j<=doors;j++) res=min(res,map[blank[i]][door[j]]); l=max(l,res); } for(i=1;i<=doors;i++) { int I=door[i],J; for(j=1;j<=blanks;j++)if(map[I][J=blank[j]]<inf) Add(j,point[i][map[I][J]],1); } for(i=1;i<=doors;i++)for(j=1;j<R;j++)Add(point[i][j],point[i][j+1],inf); for(i=1;i<=doors;i++)for(j=1;j<=R;j++) // 80*500 { Add(point[i][j],t,1); rec[i][j]=cnt-1; } return ; } bool check(int mid) { build(mid); int maxflow=0; while(bfs())maxflow+=dinic(s,inf); if(maxflow==blanks)return 1; return 0; } int main() { int l=0,r=R,mid,ans=inf; Build(l,r); while(l<=r) { if(r-l<=3) { for(int i=l;i<=r;i++)if(check(i)){ans=i;break;} break; } mid=l+r>>1; if(check(mid))r=mid; else l=mid+1; } if(ans==inf)puts("impossible"); else printf("%d\n",ans); return 0; }
时间: 2024-10-25 09:52:51