标签:ott bre cep scan size ace 接下来 archive split
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10394 Accepted Submission(s): 4617
题目大意:字面意思。。
解题思路:用村名作为左边、房子作为右边,村名对房子的利润作为边,km模板直接用就行。。(这里有很详细的讲解,我就不重复说了。。http://www.cnblogs.com/jackge/archive/2013/05/03/3057028.html)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=305; const int INF=0x3f3f3f3f; int line[maxn][maxn]; int ex_b[maxn]; int ex_g[maxn]; bool vis_b[maxn]; bool vis_g[maxn]; int n,match[maxn],slack[maxn]; bool dfs(int x) { vis_g[x] = true; for(int i=0;i<n;i++) { if(vis_b[i]) continue; int temp = ex_g[x]+ex_b[i]-line[x][i]; if(temp==0) { vis_b[i] = true; if(match[i]==-1 || dfs(match[i])) { match[i] = x; return true; } } else { slack[i] = min(slack[i], temp); } } return false; } int km() { memset(match,-1,sizeof(match)); memset(ex_b,0,sizeof(ex_b)); for(int i=0;i<n;i++) { ex_g[i]=line[i][0]; for(int j=1;j<n;j++) ex_g[i] = max(line[i][j],ex_g[i]); } for(int i=0;i<n;i++) { fill(slack,slack+n,INF); while(1) { memset(vis_g,false,sizeof(vis_g)); memset(vis_b,false,sizeof(vis_b)); if(dfs(i)) break; int d=INF; for(int j=0;j<n;j++) if(vis_b[j]==false) { d = min(d,slack[j]); } for(int j=0;j<n;j++) { if(vis_g[j]) ex_g[j] -= d; if(vis_b[j]) ex_b[j] += d; else slack[j] -= d; } } } int ret = 0; for(int i=0;i<n;i++) { ret += line[match[i]][i]; } return ret; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&line[i][j]); } } printf("%d\n",km()); } }
标签:ott bre cep scan size ace 接下来 archive split
原文地址:http://www.cnblogs.com/WWkkk/p/7412809.html