题解:CF2109F Penguin Steps
来写题解了 111
首先思考如何使初始的
那么暂时不考虑
这是一个最小割问题,但是注意到原问题是在四连通下割点,那么其对偶问题就是在八连通下的最短路,就是找到从上边界到下边界的八连通最短路径,其最短路径经过的点就是要割掉的点,跑最短路就能知道答案。
接下来就是考虑
时间复杂度
const int N = 3e2 + 5;
const int V = 1e6;
const int INF = 1e9 + 7;
int n, p, k, a[N][N];
bool b[N][N];
int pre[N][N];
string s[N];
int d1, d2;
int dx[] = {- 1, 0, 1, 0};
int dy[] = {0, 1, 0, - 1};
int c[N][N], d[N][N];
bool vis[N][N];
void dfs(int x, int y) {
int d = (pre[x][y] + 3) & 3;
for(int i = 0; i < 3; i ++, d = (d + 1) & 3) {
int px = x + dx[d], py = y + dy[d];
if(px < 1 || px > n || py < 1 || py > n) continue;
if(a[px][py] > d1 || b[px][py]) continue;
b[px][py] = 1;
pre[px][py] = d;
dfs(px, py);
}
}
void solve() {
cin >> n >> p >> k;
FOR(i, 1, n) FOR(j, 1, n) cin >> a[i][j];
FOR(i, 1, n) cin >> s[i], s[i] = ' ' + s[i];
int L = 1, R = V; d1 = 0;
while(L <= R) {
int mid = L + R >> 1;
FOR(i, 1, n) FOR(j, 1, n) b[i][j] = 0;
queue<PII> q;
if(a[1][1] <= mid) b[1][1] = 1, q.push({1, 1});
while(! q.empty()) {
int x, y; tie(x, y) = q.front(); q.pop();
REP(d, 4) {
int px = x + dx[d], py = y + dy[d];
if(px < 1 || px > n || py < 1 || py > n) continue;
if(a[px][py] > mid || b[px][py]) continue;
b[px][py] = 1;
q.push({px, py});
}
}
if(b[p][n]) {
d1 = mid;
R = mid - 1;
}
else {
L = mid + 1;
}
}
{
FOR(i, 1, n) FOR(j, 1, n) b[i][j] = 0;
b[1][1] = pre[1][1] = 1;
dfs(1, 1);
FOR(i, 1, n) FOR(j, 1, n) b[i][j] = 0;
int x = p, y = n;
while(x != 1 || y != 1) {
b[x][y] = 1;
int d = pre[x][y];
x -= dx[d], y -= dy[d];
}
b[1][1] = 1;
}
L = 1, R = V << 1, d2 = 0;
while(L <= R) {
int mid = L + R >> 1;
FOR(i, 0, n + 1) FOR(j, 0, n + 1) vis[i][j] = c[i][j] = 0;
FOR(i, 1, n) FOR(j, 1, n) if(a[i][j] < mid) {
c[i][j] = mid - a[i][j];
if(s[i][j] == '0') c[i][j] = INF;
if(b[i][j] && mid > d1) c[i][j] = INF;
}
c[n][0] = c[n + 1][1] = c[p][n + 1] = INF;
FOR(i, 0, n + 1) FOR(j, 0, n + 1) d[i][j] = INF;
d[0][0] = 0;
priority_queue<pair<int, PII>, vector<pair<int, PII>>, greater<pair<int, PII>>> pq;
pq.push({0, {0, 0}});
while(! pq.empty()) {
int x, y; tie(x, y) = SE(pq.top()); pq.pop();
if(vis[x][y]) continue;
vis[x][y] = 1;
FOR(dx, - 1, 1) FOR(dy, - 1, 1) {
int px = x + dx, py = y + dy;
if(px < 0 || px > n + 1 || py < 0 || py > n + 1) continue;
if(d[px][py] > d[x][y] + c[px][py]) {
d[px][py] = d[x][y] + c[px][py];
pq.push({d[px][py], {px, py}});
}
}
}
if(d[n + 1][n + 1] <= k) {
d2 = mid;
L = mid + 1;
}
else {
R = mid - 1;
}
}
cout << d1 << " " << d2 << endl;
}