标签:sub mem class inner div chmod arc 传说 memset
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10361 Accepted Submission(s): 4596
1 //2017-08-21 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 310; 10 const int INF = 0x3f3f3f3f; 11 int G[N][N]; 12 int n_x, n_y; 13 int matching[N], l_x[N], l_y[N]; 14 int slack[N]; 15 bool vis_x[N], vis_y[N]; 16 17 bool dfs(int u){ 18 vis_x[u] = 1; 19 for(int v = 0; v < n_y; v++){ 20 if(vis_y[v])continue; 21 int tmp = l_x[u]+l_y[v]-G[u][v]; 22 if(tmp == 0){ 23 vis_y[v] = 1; 24 if(matching[v] == -1 || dfs(matching[v])){ 25 matching[v] = u; 26 return true; 27 } 28 }else if(slack[v] > tmp){ 29 slack[v] = tmp; 30 } 31 } 32 return false; 33 } 34 35 int KM(){ 36 memset(matching, -1, sizeof(matching)); 37 memset(l_y, 0, sizeof(l_y)); 38 for(int u = 0; u < n_x; u++){ 39 l_x[u] = -INF; 40 for(int v = 0; v < n_y; v++) 41 if(G[u][v] > l_x[u]) 42 l_x[u] = G[u][v]; 43 } 44 for(int u = 0; u < n_x; u++){ 45 for(int i = 0; i < n_y; i++) 46 slack[i] = INF; 47 while(1){ 48 memset(vis_x, 0, sizeof(vis_x)); 49 memset(vis_y, 0, sizeof(vis_y)); 50 if(dfs(u))break; 51 int d = INF; 52 for(int i = 0; i < n_y; i++) 53 if(!vis_y[i] && d > slack[i]) 54 d = slack[i]; 55 for(int i = 0; i < n_x; i++) 56 if(vis_x[i]) 57 l_x[i] -= d; 58 for(int i = 0; i < n_y; i++){ 59 if(vis_y[i])l_y[i] += d; 60 else slack[i] -= d; 61 } 62 } 63 } 64 int ans = 0; 65 for(int i = 0; i < n_y; i++) 66 if(matching[i] != -1) 67 ans += G[matching[i]][i]; 68 return ans; 69 } 70 71 int main() 72 { 73 freopen("inputK.txt", "r", stdin); 74 int n; 75 while(scanf("%d", &n)!=EOF){ 76 for(int i = 0; i < n; i++){ 77 for(int j = 0; j < n; j++){ 78 scanf("%d", &G[i][j]); 79 } 80 } 81 n_x = n_y = n; 82 printf("%d\n", KM()); 83 } 84 85 return 0; 86 }
标签:sub mem class inner div chmod arc 传说 memset
原文地址:http://www.cnblogs.com/Penn000/p/7406240.html