标签:min 测试用例 rip .com bre dfs define 必须 www.
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0x3f3f3f3f 12 using namespace std; 13 const int my_max = 310; 14 15 int n, nx, ny, my_map[my_max][my_max], my_lx[my_max], my_line[my_max], 16 my_slack[my_max], my_bookx[my_max], my_booky[my_max], my_ly[my_max]; 17 18 bool my_dfs(int x) 19 { 20 my_bookx[x] = 1; 21 for (int i = 1; i <= ny; ++ i) 22 { 23 if (my_booky[i]) continue; 24 int temp = my_lx[x] + my_ly[i] - my_map[x][i]; 25 if (temp == 0) 26 { 27 my_booky[i] = 1; 28 if (!my_line[i] || my_dfs(my_line[i])) 29 { 30 my_line[i] = x; 31 return true; 32 } 33 } 34 else if (my_slack[i] > temp) 35 my_slack[i] = temp; 36 } 37 return false; 38 } 39 40 int my_km() 41 { 42 memset(my_lx, 0, sizeof(my_lx)); 43 memset(my_line, 0, sizeof(my_line)); 44 45 for (int i = 1; i <= nx; ++ i) 46 for (int j = 1; j <= ny; ++ j) 47 if (my_map[i][j] > my_lx[i]) 48 my_lx[i] = my_map[i][j]; 49 50 for (int i = 1; i <= nx; ++ i) 51 { 52 for (int j = 1; j <= ny; ++ j) 53 my_slack[j] = INF; 54 while (1) 55 { 56 memset(my_bookx, 0, sizeof(my_bookx)); 57 memset(my_booky, 0, sizeof(my_booky)); 58 59 if (my_dfs(i)) break; 60 61 int my_temp_min = INF; 62 for (int j = 1; j <= ny; ++ j) 63 if (!my_booky[j] && my_slack[j] < my_temp_min) 64 my_temp_min = my_slack[j]; 65 66 for (int j = 1; j <= nx; ++ j) 67 if (my_bookx[j]) my_lx[j] -= my_temp_min; 68 for (int j = 1; j <= ny; ++ j) 69 if (my_booky[j]) my_ly[j] += my_temp_min; 70 else my_slack[j] -= my_temp_min; 71 } 72 } 73 int my_ans = 0; 74 for (int i = 1; i <= ny; ++ i) 75 my_ans += my_map[my_line[i]][i]; 76 return my_ans; 77 } 78 79 int main() 80 { 81 while (~scanf("%d", &n)) 82 { 83 nx = ny = n; 84 for (int i = 1; i <= n; ++ i) 85 for (int j = 1; j <= n; ++ j) 86 scanf("%d", &my_map[i][j]); 87 printf("%d\n", my_km()); 88 } 89 return 0; 90 }
引之:https://www.cnblogs.com/jackge/archive/2013/05/03/3057028.html
标签:min 测试用例 rip .com bre dfs define 必须 www.
原文地址:https://www.cnblogs.com/GetcharZp/p/9471555.html