[HNOI2007]紧急疏散

二分+网络流判定
首先处理出每个人和门间的距离
二分时间,连边时把每个门拆成mid个,一个人能在mid时间内到达,他也可以在这等一会儿,那么这mid个门之间连边

如果可以在x的时间内到达,那么x~mid之间他都可以出去,所以门向门加一连边

奉上未AC代码千万别复制

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1010), __(1e6 + 10), INF(2147483647);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, mp[30][30], cntr, cntd, dis[_][_], id[_][_];
int tcnt, tmp1[_ * 100], tmp2[__], w[__], fst[_ * 100], nxt[__], to[__], cnt, S, T, lev[_ * 100], cur[_ * 100], max_flow;
queue <int> Q;

IL void Add(RG int u, RG int v, RG int f){
    w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
    w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}

IL int Dfs(RG int u, RG int maxf){
    if(u == T) return maxf;
    RG int ret = 0;
    for(RG int &e = cur[u]; e != -1; e = nxt[e]){
        if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
        RG int f = Dfs(to[e], min(w[e], maxf - ret));
        ret += f; w[e ^ 1] += f; w[e] -= f;
        if(ret == maxf) break;
    }
    return ret;
}

IL bool Bfs(){
    Fill(lev, 0); lev[S] = 1; Q.push(S);
    while(!Q.empty()){
        RG int u = Q.front(); Q.pop();
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(lev[to[e]] || !w[e]) continue;
            lev[to[e]] = lev[u] + 1;
            Q.push(to[e]);
        }
    }
    return lev[T];
}

IL int Maxflow(){  for(max_flow = 0; Bfs(); ) Copy(cur, fst), max_flow += Dfs(S, INF); return max_flow;  }

struct Data{  int x, y, d;  } u;
queue <Data> que;
int dx[] = {1, -1}, dy[] = {0, 0, 1, -1}, vis[_][_];

IL void BFS(RG int x, RG int y, RG int ID){
    Fill(vis, 0); que.push((Data){x, y}); vis[x][y] = 1;
    while(!que.empty()){
        u = que.front(); que.pop();
        for(RG int i = 0; i < 4; ++i){
            RG int xx = u.x + dx[i], yy = u.y + dy[i];
            if(xx < 1 || yy < 1 || xx > n || yy > m || mp[xx][yy] == 1 || vis[xx][yy]) continue;
            vis[xx][yy] = 1;
            if(mp[xx][yy] == 2) dis[ID][id[xx][yy]] = u.d + 1;
            que.push((Data){xx, yy, u.d + 1});
        }
    }
}

IL int Check(RG int x){
    Fill(fst, -1); cnt = 0; T = cntr + cntd * x + 1;
    for(RG int i = 1; i <= cntr; ++i) Add(S, i, 1);
    for(RG int i = 1; i <= cntd; ++i)
        for(RG int j = 1; j <= x; ++j){
            Add(cntr + x * (i - 1) + j, T, 1);
            if(j < x) Add(cntr + x * (i - 1) + j, cntr + x * (i - 1) + j + 1, INF);
        }
    for(RG int i = 1; i <= cntr; ++i)
        for(RG int j = 1; j <= cntd; ++j)
            if(dis[i][j] && dis[i][j] <= x) Add(i, cntr + x * (j - 1) + dis[i][j], INF);
    return Maxflow();
}

int main(RG int argc, RG char* argv[]){
    n = Read(); m = Read();
    for(RG int i = 1; i <= n; ++i){
        RG char c[30]; scanf(" %s", c);
        for(RG int j = 1; j <= m; ++j){
            if(c[j - 1] == '.') id[i][j] = ++cntr;
            else if(c[j - 1] == 'D') mp[i][j] = 2, id[i][j] = ++cntd;
            else mp[i][j] = 1;
        }
    }
    for(RG int i = 1; i <= n; ++i)
        for(RG int j = 1; j <= m; ++j)
            if(!mp[i][j]) BFS(i, j, id[i][j]);
    RG int l = 0, r = 400, ans = -1;
    while(l <= r){
        RG int mid = (l + r) >> 1;
        if(Check(mid) == cntr) r = mid - 1, ans = mid;
        else l = mid + 1;
    }
    ans == -1 ? puts("impossible") : printf("%d
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/cjoieryl/p/8206315.html