hdu 1533 Going Home【KM算法求最大权匹配】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533

我的链接:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=17728#problem/B

Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1895    Accepted Submission(s): 918


Problem Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. 

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
 

Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
 

Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. 
 

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
 
Recommend
lwg
 
思路将m的坐标记录到左集,h的坐标记录到右集,w[i][j]表示第i个m到第j个h的距离
          w[i][j]=△x+△y 然后因为是求最小值,而KM是求最大值
          所以只要这样:w[i][j] = -w[i][j]建图再套模板输出【-sum】就ok!

PS:思路看的网上牛人的,初学KM,代码效率较低。

//Accepted	348 KB	62 ms	C++	2069 B
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110;
char map[maxn][maxn];
int w[maxn][maxn];
bool s[maxn],t[maxn];
int match[maxn];
int lx[maxn],ly[maxn];
int n;
bool hungary(int u){
     s[u]=true;
     for(int v=1;v<=n;v++){
         if(!t[v] && lx[u]+ly[v]==w[u][v]){
            t[v]=true;//易遗忘 
            if(match[v]==-1 || hungary(match[v])){
               match[v]=u;
               return true;
            }
         }
     }
     return false;
}
int KM(){
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n;i++){
    	lx[i]=-1<<30;//注意 
    }
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
       for(int j=1;j<=n;j++)
          lx[i]=max(lx[i],w[i][j]);
    for(int i=1;i<=n;i++){
        while(1){
           memset(s,false,sizeof(s));
           memset(t,false,sizeof(t));
           if(hungary(i)) break;
           else{
                int a=1<<30;
                for(int j=1;j<=n;j++) if(s[j]){
                    for(int k=1;k<=n;k++) if(!t[k] && a>lx[j]+ly[k]-w[j][k])
                        a=lx[j]+ly[k]-w[j][k];
                }
                for(int j=1;j<=n;j++){
                   if(s[j]) lx[j]-=a;
                   if(t[j]) ly[j]+=a;
                }
           }
        }
    }
    for(int i=1;i<=n;i++) ans+=w[match[i]][i];
    return -ans;//易遗忘 
}
int main(){
	int row,col,numi,numj;
    while(scanf("%d%d%*c",&row,&col)!=EOF){
    	 if(row==0 || col==0) break;
    	 n=numi=numj=0;
         for(int i=1;i<=row;i++){
         	for(int j=1;j<=col;j++){
	         	scanf("%c",&map[i][j]);
	         	if(map[i][j]=='m') n++;
	         }
	         getchar();
         }
         for(int i=1;i<=row;i++){
         	for(int j=1;j<=col;j++){
	         	if(map[i][j]=='m'){
	         		numi++;numj=1;
	         		for(int k=1;k<=row;k++){
		         		for(int e=1;e<=col;e++){
		         			if(map[k][e]=='H')
		         			w[numi][numj++]=-(abs(i-k)+abs(j-e));
		         		}
		         	}
	         	}
	         }
         }
         printf("%d\n",KM());
    }
    return 0;
}



内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值