Game HDU - 3657
onmylove has invented a game on n × m grids. There is one positive integer on each grid. Now you can take the numbers from the grids to make your final score as high as possible. The way to get score is like
the following:
● At the beginning, the score is 0;
● If you take a number which equals to x, the score increase x;
● If there appears two neighboring empty grids after you taken the number, then the score should be decreased by 2(x&y). Here x and y are the values used to existed on these two grids. Please pay attention that "neighboring grids" means there exits and only exits one common border between these two grids.
Since onmylove thinks this problem is too easy, he adds one more rule:
● Before you start the game, you are given some positions and the numbers on these positions must be taken away.
Can you help onmylove to calculate: what's the highest score onmylove can get in the game?
the following:
● At the beginning, the score is 0;
● If you take a number which equals to x, the score increase x;
● If there appears two neighboring empty grids after you taken the number, then the score should be decreased by 2(x&y). Here x and y are the values used to existed on these two grids. Please pay attention that "neighboring grids" means there exits and only exits one common border between these two grids.
Since onmylove thinks this problem is too easy, he adds one more rule:
● Before you start the game, you are given some positions and the numbers on these positions must be taken away.
Can you help onmylove to calculate: what's the highest score onmylove can get in the game?
InputMultiple input cases. For each case, there are three integers n, m, k in a line.
n and m describing the size of the grids is n ×m. k means there are k positions of which you must take their numbers. Then following n lines, each contains m numbers, representing the numbers on the n×m grids.Then k lines follow. Each line contains two integers, representing the row and column of one position
and you must take the number on this position. Also, the rows and columns are counted start from 1.
Limits: 1 ≤ n, m ≤ 50, 0 ≤ k ≤ n × m, the integer in every gird is not more than 1000.OutputFor each test case, output the highest score on one line.
Sample Input
2 2 1 2 2 2 2 1 1 2 2 1 2 7 4 1 1 1
Sample Output
4
9
Hint
As to the second case in Sample Input, onmylove gan get the highest score when calulating like this:
2 + 7 + 4 - 2 × (2&4) - 2 × (2&7) = 13 - 2 × 0 - 2 × 2 = 9.
题解:首先为什么是二分图,因为题目中有“相邻的两个点”!!!哈哈,神奇吧。还有要明白为什么是最大匹配。总之是炒鸡好题!
1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include<queue> 6 #include<cstdio> 7 #include<cstring> 8 #include<iostream> 9 #include<algorithm> 10 using namespace std; 11 12 const int maxn = 100100; 13 const int maxm = 500100; 14 const int INF = 0x3f3f3f3f; 15 16 struct edge { 17 int to, cap, next; 18 }e[2*maxm]; 19 20 int n, m, k, tot, src, des; 21 int head[maxn], dep[maxn], map[110][110]; 22 23 void Inite() { 24 des = n*m + 1; 25 src = tot = 0; 26 memset(head, -1, sizeof(head)); 27 } 28 29 void addedge(int u, int v, int w) { 30 e[tot].to = v, e[tot].cap = w, e[tot].next = head[u], head[u] = tot++; 31 e[tot].to = u, e[tot].cap = 0, e[tot].next = head[v], head[v] = tot++; 32 } 33 34 int BFS() { 35 memset(dep, -1, sizeof(dep)); 36 dep[src] = 0; 37 queue<int> q; 38 q.push(src); 39 while (!q.empty()) { 40 int u = q.front(); 41 q.pop(); 42 for (int i = head[u]; i != -1; i = e[i].next) { 43 int v = e[i].to; 44 if (e[i].cap > 0 && dep[v] == -1) { 45 dep[v] = dep[u] + 1; 46 q.push(v); 47 } 48 } 49 } 50 return dep[des] != -1; 51 } 52 53 int DFS(int u, int minx) { 54 if (u == des) return minx; 55 int temp; 56 for (int i = head[u]; i != -1; i = e[i].next) { 57 int v = e[i].to; 58 if (e[i].cap > 0 && dep[v] == dep[u] + 1 && (temp = DFS(v, min(minx, e[i].cap)))) { 59 e[i].cap -= temp; 60 e[i ^ 1].cap += temp; 61 return temp; 62 } 63 } 64 return 0; 65 } 66 67 int Dinic() { 68 int ans = 0, temp; 69 while (BFS()) { 70 while (true) { 71 temp = DFS(src, INF); 72 if (temp == 0) break; 73 ans += temp; 74 } 75 } 76 return ans; 77 } 78 79 int main() 80 { 81 while (scanf_s("%d%d%d", &n, &m, &k) != EOF) { 82 Inite(); 83 int sum = 0; 84 for (int i = 1; i <= n; i++) { 85 for (int j = 1; j <= m; j++) { 86 scanf_s("%d", &map[i][j]); 87 sum += map[i][j]; 88 } 89 } 90 for (int i = 1; i <= k; i++) { 91 int x, y; 92 scanf_s("%d%d", &x, &y); 93 if ((x + y) % 2 == 0) 94 addedge(src, (x - 1)*m + y, INF); 95 else 96 addedge((x - 1)*m + y, des, INF); 97 } 98 for (int i = 1; i <= n; i++) { 99 for (int j = 1; j <= m; j++) { 100 int temp = (i - 1)*m + j; 101 if ((i + j) % 2 == 0) 102 addedge(src, temp, map[i][j]); 103 else 104 addedge(temp, des, map[i][j]); 105 } 106 } 107 for (int i = 1; i <= n; i++) { 108 for (int j = 1; j <= m; j++) { 109 if ((i + j) % 2) continue; 110 int temp = (i - 1)*m + j; 111 if (i > 1) addedge(temp, temp - m, 2 * (map[i][j] & map[i - 1][j])); 112 if (i < n) addedge(temp, temp + m, 2 * (map[i][j] & map[i + 1][j])); 113 if (j > 1) addedge(temp, temp - 1, 2 * (map[i][j] & map[i][j - 1])); 114 if (j < m) addedge(temp, temp + 1, 2 * (map[i][j] & map[i][j + 1])); 115 } 116 } 117 printf_s("%d\n", sum - Dinic()); 118 } 119 return 0; 120 }

浙公网安备 33010602011771号