标签:time 模板 ted limit problem iss 必须 names 输入
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10760 Accepted Submission(s): 4765
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 #define ll long long 8 #define mod 998244353 9 const int N=310; 10 const int INF=0x3f3f3f3f; 11 int nx,ny; 12 int g[N][N]; 13 int linker[N],lx[N],ly[N]; 14 int slack[N]; 15 bool visx[N],visy[N]; 16 bool dfs(int x){ 17 visx[x]=true; 18 for(int y=0;y<ny;y++){ 19 if(visy[y]) continue; 20 int tmp=lx[x]+ly[y]-g[x][y]; 21 if(tmp==0){ 22 visy[y]=true; 23 if(linker[y]==-1||dfs(linker[y])){ 24 linker[y]=x; 25 return true; 26 } 27 }else if(slack[y]>tmp){ 28 slack[y]=tmp; 29 } 30 } 31 return false; 32 } 33 int KM(){ 34 memset(linker,-1,sizeof(linker)); 35 memset(ly,0,sizeof(ly)); 36 for(int i=0;i<nx;i++){ 37 lx[i]=-INF; 38 for(int j=0;j<ny;j++){ 39 if(g[i][j]>lx[i]){ 40 lx[i]=g[i][j]; 41 } 42 } 43 } 44 for(int x=0;x<nx;x++){ 45 for(int i=0;i<ny;i++) 46 slack[i]=INF; 47 while(true){ 48 memset(visx,false,sizeof(visx)); 49 memset(visy,false,sizeof(visy)); 50 if(dfs(x)) break; 51 int d=INF; 52 for(int i=0;i<ny;i++){ 53 if(!visy[i]&&d>slack[i]) 54 d=slack[i]; 55 } 56 for(int i=0;i<nx;i++){ 57 if(visx[i]) 58 lx[i]-=d; 59 } 60 for(int i=0;i<ny;i++){ 61 if(visy[i]) ly[i]+=d; 62 else slack[i]-=d; 63 } 64 } 65 } 66 int res=0; 67 for(int i=0;i<ny;i++){ 68 if(linker[i]!=-1) 69 res+=g[linker[i]][i]; 70 71 } 72 return res; 73 } 74 int main(){ 75 int n; 76 while(scanf("%d",&n)!=EOF){ 77 for(int i=0;i<n;i++){ 78 for(int j=0;j<n;j++){ 79 scanf("%d",&g[i][j]); 80 } 81 } 82 nx=ny=n; 83 printf("%d\n",KM()); 84 } 85 return 0; 86 }
标签:time 模板 ted limit problem iss 必须 names 输入
原文地址:http://www.cnblogs.com/hsd-/p/7638547.html