题意:
n*m的黑白格子,填数字,使白色区域的行列值的和等于有值的黑色区域的相对应的值。
思路:
每个白格子数字可以填1-9,暗示了流量必须>=1。所以预先设流量为1,相当于容量减1,原先的容量为9,现在为8。
最大流,建图轮廓为 S ——》行的和 ——》白格子——》列的和 ——》T.
由于行的和——》白格子,白格子——》列的和,的容量都减1,所以S——》行的和,列的和——》T的容量也要相应的减小。
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 20000+5;
using namespace std;
int n,m;
char ch[10];
int G1[105][105];
// 图
struct Edge{
int u, v, cap, flow;
Edge(int a, int b, int c, int d):u(a),v(b),cap(c),flow(d){}
};
vector<Edge> edges;
vector<int> G[maxn];
int dis[maxn]; // 分层的编号
int cur[maxn]; // 当前弧,重要优化!!!
void init(){
for(int i = 0; i < 20001; ++i) G[i].clear();
edges.clear();
}
void addEdge(int u, int v, int cap){
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0)); // 反向弧
int m = edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
// 分层
bool bfs(int s, int t){
memset(dis, -1, sizeof(dis));
dis[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); ++i){
Edge e = edges[G[x][i]];
if(dis[e.v] == -1&&e.cap > e.flow){
dis[e.v] = dis[x] + 1;
Q.push(e.v);
}
}
}
return dis[t] != -1;
}
int dfs(int s, int t, int cur_flow){
if(s == t||cur_flow == 0) return cur_flow;
int ans = 0;
for(int& i = cur[s]; i < G[s].size(); ++i){
int c = G[s][i];
Edge e = edges[c];
if(dis[e.v] == dis[s] + 1&&e.cap > e.flow){
int a2 = min(cur_flow, e.cap-e.flow);
int w = dfs(e.v, t, a2);
edges[c].flow += w;
edges[c^1].flow -= w;
cur_flow -= w;
ans += w;
if(cur_flow <= 0) break;
}
}
return ans;
}
// 最大流
int Dinic(int s, int t){
int ans = 0;
while(bfs(s,t)){
memset(cur, 0, sizeof(cur));
ans += dfs(s,t,INF);
}
return ans;
}
struct Sum{
int x,y,s;
}R[maxn], C[maxn];
int calc(int a, int b){
int p = a+b;
return edges[G[p][1]].flow + 1;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) == 2){
init();
int tot = 0, r_cnt = 0, c_cnt = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
scanf("%s",ch);
if(ch[0] == '.') G1[i][j] = ++tot;
else{
G1[i][j] = -1;
if(ch[0] != 'X'){ C[++c_cnt].x = i; C[c_cnt].y = j; C[c_cnt].s = (ch[0]-'0')*100 + (ch[1]-'0')*10 + ch[2]-'0'; }
if(ch[4] != 'X'){ R[++r_cnt].x = i; R[r_cnt].y = j; R[r_cnt].s = (ch[4]-'0')*100 + (ch[5]-'0')*10 + ch[6]-'0'; }
}
}
int s = 0, t = r_cnt + c_cnt + tot + 1;
for(int i = 1; i <= r_cnt; ++i){
int x = R[i].x, y = R[i].y;
int white_num = 0;
for(y = y+1; y <= m; ++y) {
if(G1[x][y] != -1){
addEdge(i, r_cnt+G1[x][y], 8);
++white_num;
}
else break;
}
addEdge(s, i, R[i].s - white_num);
}
for(int i = 1; i <= c_cnt; ++i){
int x = C[i].x, y = C[i].y;
int white_num = 0;
for(x = x+1; x <= n; ++x){
if(G1[x][y] != -1){
addEdge(r_cnt+G1[x][y], r_cnt+tot+i, 8);
++white_num;
}
else break;
}
addEdge(r_cnt+tot+i, t, C[i].s - white_num);
}
int ans = Dinic(s, t);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j){
if(G1[i][j] == -1) printf("_");
else printf("%d", calc(r_cnt, G1[i][j]));
printf("%c",j == m? '\n':' ');
}
}
fclose(stdin);
return 0;
}