标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5029 Accepted Submission(s): 2160
1 #include <bits/stdc++.h> 2 #define type int 3 using namespace std; 4 const int INF = 0x3f3f3f3f; 5 const int maxn = 310; 6 type Lx[maxn],Ly[maxn],W[maxn][maxn],slack[maxn]; 7 bool S[maxn],T[maxn]; 8 int n,Link[maxn]; 9 bool match(int u) { 10 S[u] = true; 11 for(int v = 0; v < n; ++v) { 12 if(T[v]) continue; 13 type d = Lx[u] + Ly[v] - W[u][v]; 14 if(!d) { //浮点数时需要用精度判0 15 T[v] = true; 16 if(Link[v] == -1 || match(Link[v])) { 17 Link[v] = u; 18 return true; 19 } 20 } else if(slack[v] > d) slack[v] = d; 21 } 22 return false; 23 } 24 void update() { 25 type d = INF; 26 for(int i = 0; i < n; ++i) 27 if(!T[i] && slack[i] < d) 28 d = slack[i]; 29 for(int i = 0; i < n; ++i) { 30 if(S[i]) Lx[i] -= d; 31 if(T[i]) Ly[i] += d; 32 else slack[i] -= d; 33 } 34 } 35 type KuhnMunkras() { 36 for(int u = 0; u < n; ++u) { 37 Lx[u] = -INF; 38 Ly[u] = 0; 39 Link[u] = -1; 40 for(int v = 0; v < n; ++v) 41 Lx[u] = max(Lx[u],W[u][v]); 42 } 43 for(int u = 0; u < n; ++u){ 44 for(int v = 0; v < n; ++v) slack[v] = INF; 45 while(true){ 46 memset(S,false,sizeof S); 47 memset(T,false,sizeof T); 48 if(match(u)) break; 49 update(); 50 } 51 } 52 type ret = 0; 53 for(int i = 0; i < n; ++i) 54 if(Link[i] > -1) ret += W[Link[i]][i]; 55 return ret; 56 } 57 int main() { 58 while(~scanf("%d",&n)) { 59 memset(W,0,sizeof W); 60 for(int i = 0; i < n; ++i) 61 for(int j = 0; j < n; ++j) 62 scanf("%d",W[i] + j); 63 printf("%d\n",KuhnMunkras()); 64 } 65 return 0; 66 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4678390.html