标签:des style http color os io ar strong for
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18094 | Accepted: 9225 |
Description
Input
Output
Sample Input
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
Sample Output
2 10 28
Source
AC代码:
#include<stdio.h> #include<iostream> #include<math.h> #include<cstring> #include<algorithm> using namespace std; struct Node{ int x,y; }house[105],man[105]; int dis(Node t1,Node t2){ return abs(t1.x-t2.x)+abs(t1.y-t2.y); } int N; int w[105][105]; int lx[105],ly[105],link[105]; int s[105],t[105]; int match(int i){ s[i]=1; for(int j=1;j<=N;j++){ if(lx[i]+ly[j]==w[i][j] && !t[j]){ t[j]=1; if(!link[j] || match(link[j])){ link[j]=i; return 1; } } } return 0; } void update(){ int a=1<<20; for(int i=1;i<=N;i++){ if(s[i]){ for(int j=1;j<=N;j++){ if(!t[j]){ a=min(a,lx[i]+ly[j]-w[i][j]); } } } } for(int i=1;i<=N;i++){ if(s[i]) lx[i]-=a; if(t[i]) ly[i]+=a; } } int KM(){ for(int i=1;i<=N;i++){ link[i]=ly[i]=0; lx[i]=-999999; for(int j=1;j<=N;j++){ lx[i]=max(lx[i],w[i][j]); } } for(int i=1;i<=N;i++){ while(1){ for(int j=1;j<=N;j++) s[j]=t[j]=0; if(match(i)) break; else update(); } } int ret=0; for(int i=1;i<=N;i++){ //取反 if(link[i]){ ret-=w[link[i]][i]; } } return ret; } int main(){ int n,m; while(scanf("%d%d",&n,&m)==2){ if(n==0 && m==0) break; int k1,k2; k1=k2=0; for(int i=1;i<=n;i++){ char ch[105]; scanf("%s",ch); for(int j=0;j<m;j++){ if(ch[j]=='H'){ house[++k1].x=i; house[k1].y=j+1; } else if(ch[j]=='m'){ man[++k2].x=i; man[k2].y=j+1; } } } N=k1; memset(w,0,sizeof(w)); for(int i=1;i<=N;i++) //建图 for(int j=1;j<=N;j++){ w[i][j]-=dis(house[i],man[j]); //要求最小权,所以保存负数,KM后取反就是最小权了 } int ans=KM(); printf("%d\n",ans); } return 0; }
标签:des style http color os io ar strong for
原文地址:http://blog.csdn.net/my_acm/article/details/39099677