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
| #include <cstdio> #include <vector> #include <cstring> #include<algorithm> #include <queue> using namespace std; const int MAXV = 20050; const int maxm=100050; const int INF = 1<<30; int edge; int level[MAXV]; int iter[MAXV]; int head[MAXV]; struct edgenode { int from; int to; int cap; int next; } edges[maxm]; void init() { edge=0; memset(head,-1,sizeof(head)); } void addedge(int from, int to, int cap) { edges[edge].from=from; edges[edge].cap=cap; edges[edge].to=to; edges[edge].next=head[from]; head[from]=edge++; edges[edge].from=to; edges[edge].cap=0; edges[edge].to=from; edges[edge].next=head[to]; head[to]=edge++; }
void bfs(int s) { memset(level, -1, sizeof level); std::queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = head[v]; i !=-1; i=edges[i].next) { edgenode &e = edges[i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } }
int dfs(int v, int t, int f) { if (v == t) return f; if(iter[v]==0) { iter[v]=head[v]; } for (int &i = iter[v]; i !=-1; i=edges[i].next) { edgenode &e = edges[i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, std::min(f, e.cap)); if (d > 0) { e.cap -= d; edges[i^1].cap += d; return d; } } } return 0; }
int maxflow(int s, int t) { int flow = 0; for (; ;) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof iter); int f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } return flow; } int row[120][120],col[120][120],name[120][120],pos; int todigit(char *dig) { return (dig[0]-'0')*100+(dig[1]-'0')*10+(dig[2]-'0'); } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(row,-1,sizeof(row)); memset(col,-1,sizeof(col)); memset(name,-1,sizeof(name)); init(); pos=0; char item[10]; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { scanf("%s",item); if(item[0]=='.') { name[i][j]=pos; addedge(pos*2,pos*2+1,8); pos++; } else { if(item[0]!='X') { col[i][j]=todigit(&item[0]); } if(item[4]!='X') { row[i][j]=todigit(&item[4]); } } } } int start=pos*2,end=pos*2+1,cons=pos*2+2; for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { if(row[i][j]!=-1) { int cotmp=j+1,num=0; while(cotmp<m&&name[i][cotmp]!=-1) { addedge(cons,name[i][cotmp]*2,INF); ++cotmp,++num; } addedge(start,cons,row[i][j]-num); ++cons; } if(col[i][j]!=-1) { int rotmp=i+1,num=0; while(rotmp<n&&name[rotmp][j]!=-1) { addedge(name[rotmp][j]*2+1,cons,INF); ++rotmp,++num; } addedge(cons,end,col[i][j]-num); ++cons; } } } int ans=maxflow(start,end); for(int i=0;i<n;++i) { for(int j=0;j<m;++j) { if(name[i][j]==-1) { printf("_ "); } else { printf("%d ",9-edges[name[i][j]*2].cap); } } printf("\n"); } } return 0; }
|