int n, m; // X ,Y 的大小 int weight[MAX_X][MAX_Y]; // X 到 Y 的映射(权重) int lx[MAX_X], ly[MAX_Y]; // 标号 bool sx[MAX_X], sy[MAX_Y]; // 是否被搜索过 int match[MAX_Y]; // Y(i) 与 X(match [i]) 匹配
voidinit(){ for(int i = 0; i < n; i ++) for(int j = 0; j < m; j ++) scanf("%d", &weight[i][j]); }
//寻找增广路径 boolhungary(int u){ sx[u] = true; for(int v = 0; v < m; v ++) if(!sy [v] && lx[u] + ly[v] == weight[u][v]){ sy[v] = true; if(match[v] == -1 || hungary(match[v])){ match [v] = u; returntrue; } } returnfalse; }
intKM(bool maxsum){ // 参数 maxsum 为 true ,返回最大权匹配,否则最小权匹配 int i, j; if(!maxsum){ for(i = 0; i < n; i ++) for(j = 0; j < m; j ++) weight [i] [j] = -weight [i] [j]; }
// 初始化标号 memset(ly, 0, sizeof(ly)); for(i = 0; i < n; i ++){ lx[i] = -0x7FFFFFFF; for(j = 0; j < m; j ++) lx[i] = max(weight[i][j], lx[i]); }
memset(match, -1, sizeof(match)); for(int u = 0; u < n; u ++){ while(1){ memset(sx, 0, sizeof(sx)); memset(sy, 0, sizeof(sy)); if(hungary(u)) break; // 修改标号 int d = 0x7FFFFFFF; for(i = 0; i < n; i++) if(sx[i]) for(j = 0; j < m; j ++) if(!sy[j]) d = min(lx[i] + ly[j] - weight[i][j], d); for(i = 0; i < n; i ++) if(sx[i]) lx[i] -= d; for(i = 0; i < m; i ++) if(sy[i]) ly[i] += d;
} } int sum = 0; for(i = 0; i < m; i ++) sum += weight[ match[i] ][i]; if(!maxsum){ sum = -sum; for(i = 0; i < n; i ++) for(j = 0; j < m; j ++) weight[i][j] = -weight[i][j]; // 如果需要保持 weight [ ] [ ] 原来的值,这里需要将其还原 } return sum; }
intmain(){ while(~scanf("%d %d", &n, &m)){ init(); printf("%d\n", KM(false)); for(int i = 0; i < m; i ++) printf("X %d -> Y %d\n", match [i], i); } return0; }
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a ‘.’ means an empty space, an ‘H’ represents a house on that point, and am ‘m’ indicates
there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
structpoint{ int x; int y; }px[MAXN],py[MAXN]; char map[MAXN]; int r, c, n, m; int weight[MAXN][MAXN]; int lx[MAXN], ly[MAXN]; bool sx[MAXN], sy[MAXN]; int match[MAXN];
voidinit(){ n = 0; m = 0; for(int i = 0; i < r; i ++){ scanf("%s", map); for(int j = 0; j < c; j ++) if(map[j] == 'H') px[n].x = i, px[n++].y = j; elseif(map[j] == 'm') py[m].x = i, py[m++].y = j; } for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) weight[i][j] = -(abs(px[i].x - py[j].x) + abs(px[i].y - py[j].y)); }
boolhungary(int u){ sx[u] = true; for(int v = 0; v < m; v ++) if(!sy [v] && lx[u] + ly[v] == weight[u][v]){ sy[v] = true; if(match[v] == -1 || hungary(match[v])){ match [v] = u; returntrue; } } returnfalse; }
intKM(){ int i, j;
memset(ly, 0, sizeof(ly)); for(i = 0; i < n; i ++){ lx[i] = -0x7FFFFFFF; for(j = 0; j < m; j ++) lx[i] = max(weight[i][j], lx[i]); }
memset(match, -1, sizeof(match)); for(int u = 0; u < n; u ++){ while(1){ memset(sx, 0, sizeof(sx)); memset(sy, 0, sizeof(sy)); if(hungary(u)) break;
int d = 0x7FFFFFFF; for(i = 0; i < n; i++) if(sx[i]) for(j = 0; j < m; j ++) if(!sy[j]) d = min(lx[i] + ly[j] - weight[i][j], d); for(i = 0; i < n; i ++) if(sx[i]) lx[i] -= d; for(i = 0; i < m; i ++) if(sy[i]) ly[i] += d; } } int sum = 0; for(i = 0; i < m; i ++) sum += weight[ match[i] ][i]; return -sum; }