1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
| import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
/**
* @author jaeji
*
*/
class edge{
int to;
int flow;
int cap;
edge rever;
edge(){
this(0,0,0,null);
}
edge(int ar_to, int ar_flow, int ar_cap, edge ar_rever){
to = ar_to;
flow = ar_flow;
cap = ar_cap;
rever = ar_rever;
}
int residu() {
return cap-flow;
}
void addflow(int ar_flow) {
flow+=ar_flow;
rever.flow -=ar_flow;
}
}
public class Main {
public static void main(String[] args) throws IOException {
int K=0,N=0,M=0;
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
//BufferedReader br = new BufferedReader( new FileReader("input.txt" ) );
StringTokenizer st = new StringTokenizer(br.readLine());
ArrayList<edge>[] adj = new ArrayList[2502];
int[] grid = new int[2502];
int[] prev = new int[2502];
edge f_edge = null;
edge b_edge = null;
K = Integer.parseInt(st.nextToken());
for(int i=0;i<K;i++) { //테스트 케이스 K번
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
Arrays.fill(grid, -1);
int s = 0;
int S = N*M;
int E = N*M+1;
int temp_curr = 0;
for(int j=0;j<N*M+2;j++) {
adj[j] = new ArrayList<edge>();
}
for(int j=0;j<N;++j) { // 엣지 입력 받기
st = new StringTokenizer(br.readLine());
for(int k=0;k<M;++k) {
temp_curr = j*M+k;
grid[temp_curr] = Integer.parseInt(st.nextToken());
s+=grid[temp_curr];
//시작점 셋팅
if((j+k)%2==0) {
f_edge = new edge(temp_curr,0,grid[temp_curr],null);
b_edge = new edge(S,0,0,f_edge);
f_edge.rever = b_edge;
adj[S].add(f_edge);
adj[temp_curr].add(b_edge);
//오른쪽 엣지
if(k<M-1) {
f_edge = new edge(temp_curr+1,0,1000,null);
b_edge = new edge(temp_curr,0,0,f_edge);
f_edge.rever = b_edge;
adj[temp_curr].add(f_edge);
adj[temp_curr+1].add(b_edge);
}
//왼쪽 엣지
if(k>0) {
f_edge = new edge(temp_curr-1,0,1000,null);
b_edge = new edge(temp_curr,0,0,f_edge);
f_edge.rever = b_edge;
adj[temp_curr].add(f_edge);
adj[temp_curr-1].add(b_edge);
}
//위쪽 엣지
if(j>0) {
f_edge = new edge((j-1)*M+k,0,1000,null);
b_edge = new edge(temp_curr,0,0,f_edge);
f_edge.rever = b_edge;
adj[temp_curr].add(f_edge);
adj[(j-1)*M+k].add(b_edge);
}
//아래쪽 엣지
if(j<N-1) {
f_edge = new edge((j+1)*M+k,0,1000,null);
b_edge = new edge(temp_curr,0,0,f_edge);
f_edge.rever = b_edge;
adj[temp_curr].add(f_edge);
adj[(j+1)*M+k].add(b_edge);
}
}
// 도착점 셋팅
else {
f_edge = new edge(E,0,grid[temp_curr],null);
b_edge = new edge(temp_curr,0,0,f_edge);
f_edge.rever = b_edge;
adj[temp_curr].add(f_edge);
adj[E].add(b_edge);
}
}
}
int curr = 0;
edge next = null;
int total = 0;
while(true) {
Arrays.fill(prev, -1);
edge[] path = new edge[2502];
Queue<Integer> q = new LinkedList<Integer>();
q.add(S);
while(!q.isEmpty()) {
curr = q.poll();
for(int j=0;j<adj[curr].size();j++) {
next = (edge)adj[curr].get(j);
if(next.residu() > 0 && prev[next.to] == -1) {
q.add(next.to);
prev[next.to] = curr;
path[next.to] = next;
if(next.to == E) break;
}
}
}
if(prev[E] == -1) break;
int flow = Integer.MAX_VALUE;
for(int j=E; j!=S; j=prev[j])
flow = Math.min(flow, path[j].residu());
for(int j=E; j!=S; j=prev[j]) {
path[j].addflow(flow);
}
total += flow;
}
System.out.println(s-total);
}
}
}
|