日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客contest897 D-Bamboo Rat(二分+黑白染色+最小割)

發(fā)布時間:2024/4/18 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客contest897 D-Bamboo Rat(二分+黑白染色+最小割) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接

題意

N×MN×MN×M的矩陣選擇KKK個數(shù)相鄰的數(shù)字不能同時選擇,讓最小的數(shù)字最大。

思路

二分枚舉答案,對于每個答案,DinicDinicDinic判斷可行性。

#include <bits/stdc++.h> #define LL long long #define P pair<int, int> #define lowbit(x) (x & -x) #define mem(a, b) memset(a, b, sizeof(a)) #define mid ((l + r) >> 1) #define lc rt<<1 #define rc rt<<1|1 #define endl '\n' const int maxn = 1e3 + 1; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; using namespace std; struct ac{int v, c, pre; }edge[maxn<<8]; int s, e; int head[maxn<<2], dis[maxn+10], curedge[maxn<<2], cnt; void init() {mem(head, -1);cnt = 0; } void addedge(int u, int v, int c) { // 記得雙向邊edge[cnt] = {v, c, head[u]};head[u] = cnt++; } bool bfs() {queue<int> que;que.push(s);mem(dis, 0);dis[s] = 1;while (!que.empty()) {int f = que.front();que.pop();for (int i = head[f]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] || edge[i].c == 0) continue;dis[edge[i].v] = dis[f] + 1;que.push(edge[i].v);}}return dis[e] > 0; }int dfs(int now, int flow) {if (now == e || flow == 0) return flow;for (int &i = curedge[now]; i != -1; i = edge[i].pre) { // 當(dāng)前弧優(yōu)化if (dis[edge[i].v] != dis[now] + 1 || edge[i].c == 0) continue;int d = dfs(edge[i].v, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i^1].c += d;return d;} }dis[now] = -1; // // 炸點優(yōu)化return 0; } int Dinic() {int sum = 0, d;while (bfs()) {for (int i = 0; i <= e; ++i) curedge[i] = head[i];while (d = dfs(s, inf)) sum += d;}return sum; } int a[maxn][maxn]; int n, m, k; int odd(int x, int y) {return ((x + y) % 2); } int pos(int x, int y) {return (x * m + y + 1); } int judge(int x, int y) {if (x < 0 || y < 0 || x >= n || y >= m) return 1;if (odd(x, y)) return 1;return 0; } int check(int x) {int sum = 0;init();s = 0, e = n * m + 1;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (a[i][j] < x) continue;sum++;if (odd(i, j)) {addedge(s, pos(i, j), 1);addedge(pos(i, j), s, 0);for (int k = -1; k <= 1; ++k) {for (int h = -1; h <= 1; ++h) {if (abs(k) + abs(h) != 1) continue;int ii = i + k;int jj = j + h;if (judge(ii, jj)) continue;addedge(pos(i, j), pos(ii, jj), inf);addedge(pos(ii, jj), pos(i, j), 0);}}}else {addedge(pos(i, j), e, 1);addedge(e, pos(i, j), 0);}}}if (sum - Dinic() >= k) return 1;return 0; } int main () {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);int T;scanf("%d", &T);while (T--) {scanf("%d%d%d", &n, &m, &k);for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {scanf("%d", &a[i][j]);}}int l = 1, r = 1000;while (l <= r) {if (check(mid)) l = mid + 1;else r = mid - 1;}printf("%d\n", r);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的牛客contest897 D-Bamboo Rat(二分+黑白染色+最小割)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。