2195 Going Home

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8986 Accepted: 4618

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

最大权二分匹配
#include<stdio.h>
#include<string>
#include<cmath>
int g[105][105];//邻接矩阵
int m[105][2];//存储人所在位置的坐标
int h[105][2];//存储房子所在位置的坐标
int lx[105],ly[105];//顶点标号
bool sx[105],sy[105];//是否已经搜索过
int link[105],n;
char ss[105][105];
int min(int a,int b)
{
    if(a<b)  return a;
    return b;
}
bool path(int k)//从x[k]寻找增广路
{
    int i;
    sx[k]=true;
    for(i=0;i<n;i++)
    {
        if(!sy[i]&&(lx[k]+ly[i]==g[k][i]))
        {
             sy[i]=1;
             if(link[i]==-1||path(link[i]))
             {
                  link[i]=k;
                  return true;
             }
        }
    }
    return false;
}
int BestMatch()//求解最小权匹配
{
    int d,sum;
    memset(ly,0,sizeof(ly));
    memset(link,-1,sizeof(link));
    for(int i=0;i<n;i++)
    {
         lx[i]=-1; //x中顶点i的编号为与i关联的Y中边的最大权重
         for(int j=0;j<n;j++)
           if(lx[i]<g[i][j])  lx[i]=g[i][j];
    }
    for(int k=0;k<n;k++)
    {
        while(1)
        {
           memset(sx,0,sizeof(sx));
           memset(sy,0,sizeof(sy));
           if(path(k))  break;//匹配成功
           d=1<<25-1;
           for(int i=0;i<n;i++)
             if(sx[i])
               for(int j=0;j<n;j++)
                 if(!sy[j])
                   d=min(d,lx[i]+ly[j]-g[i][j]);
            for(int i=0;i<n;i++)
            {
                if(sx[i]) lx[i]-=d;
                if(sy[i]) ly[i]+=d;
            }
        }
    }
    sum=0;
    for(int i=0;i<n;i++)  sum+=g[link[i]][i];
    //注意这里本来是求的最大权匹配,这里取反即为最小权匹配(因为临街矩阵中的边的权值也取反了)
    return -sum;
}
int main()
{
    int mi,hi,M,N,ans;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        if(N==0&&M==0) break;
        mi=hi=0;
        for(int i=0;i<N;i++)
        {
            scanf("%s",ss[i]);
            for(int j=0;j<M;j++)
            {
                if(ss[i][j]=='m')
                {
                    m[mi][0]=i;
                    m[mi][1]=j;
                    mi++;
                }
                else if(ss[i][j]=='H')
                {
                    h[hi][0]=i;
                    h[hi][1]=j;
                    hi++;
                }
            }
        }
        n=mi;
        for(int i=0;i<n;i++)
         for(int j=0;j<n;j++)
           g[i][j]=-(abs(m[i][0]-h[j][0])+abs(m[i][1]-h[j][1]));//即将边的权值取反
        printf("%d/n",BestMatch());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是由于在路由守卫中进行了重定向而导致的。可能是因为用户未经身份验证就尝试访问需要身份验证的页面,或者是因为用户已经登录但仍然被重定向到登录页面。为了解决这个问题,你可以检查以下几个方面: 1.确保你的路由守卫中没有重定向到同一个页面,这会导致无限循环重定向。 2.确保你的路由守卫中的重定向逻辑正确,例如在用户未经身份验证时重定向到登录页面。 3.确保你的路由配置正确,例如确保你的路由路径和组件名称正确匹配。 4.如果你使用了token进行身份验证,确保你在登录成功后将token信息放入localStorage(或sessionStorage)中,然后再进行路由跳转。 以下是一个示例代码,演示如何在路由守卫中进行身份验证和重定向: ```javascript import router from './router' import { getToken } from './utils/auth' const whiteList = ['/login'] // 不需要身份验证的页面 router.beforeEach(async(to, from, next) => { const hasToken = getToken() if (hasToken) { // 如果已经登录 if (to.path === '/login') { // 如果访问的是登录页面 next({ path: '/' }) // 重定向到首页 } else { next() // 放行 } } else { // 如果未经身份验证 if (whiteList.indexOf(to.path) !== -1) { // 如果访问的是不需要身份验证的页面 next() // 放行 } else { next(`/login?redirect=${to.path}`) // 重定向到登录页面,并将目标页面路径作为参数传递 } } }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值