poj 2195 Going Home km算法
来源:互联网 发布:笔记本 外接显卡 知乎 编辑:程序博客网 时间:2024/06/06 07:35
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.
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
Source
Pacific Northwest 2004
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define INF 99999999
#define maxn 330
int Map[maxn][maxn];
int lx[maxn],ly[maxn],match[maxn];
int visx[maxn],visy[maxn],w[maxn][maxn];
int slack[maxn];
struct ele
{
int x;
int y;
}px[maxn],py[maxn];
bool findpath(int x,int n)
{
visx[x]=1;
int y;
for(y=1;y<=n;y++)
{
if(visy[y])
continue;
if(lx[x]+ly[y]==w[x][y])
{
visy[y]=1;
if(!match[y]||findpath(match[y],n))
{
match[y]=x;
return true;
}
}
slack[y]=min(slack[y],lx[x]+ly[y]-w[x][y]);
}
return false;
}
void km(int n)
{
int i,j,temp;
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(match,0,sizeof(match));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
lx[i]=max(lx[i],w[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
slack[j]=INF;
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(findpath(i,n))
break;
temp=INF;
for(j=1;j<=n;j++)
if(!visy[j])
if(temp>slack[j])
temp=slack[j];
if(temp==INF)
break;
for(j=1;j<=n;j++)
{
if(visx[j])
lx[j]-=temp;
if(visy[j])
ly[j]+=temp;
else
slack[j]-=temp;
}
}
}
}
int main()
{
int ans;
int n,m;
int i,j;
char c;
int x,y;
while(scanf("%d%d",&n,&m),n&&m)
{
ans=0;
x=y=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>c;
if(c=='m')
{
px[x].x=i;
px[x].y=j;
x++;
}
if(c=='H')
{
py[y].x=i;
py[y].y=j;
y++;
}
}
for(i=1;i<x;i++)
for(j=1;j<y;j++)
w[i][j]=-(abs(px[i].x-py[j].x)+abs(px[i].y-py[j].y));
km(x-1);
for(i=1;i<x;i++)
ans+=w[match[i]][i];
printf("%d\n",-ans);
}
return 0;
}
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define INF 99999999
#define maxn 330
int Map[maxn][maxn];
int lx[maxn],ly[maxn],match[maxn];
int visx[maxn],visy[maxn],w[maxn][maxn];
int slack[maxn];
struct ele
{
int x;
int y;
}px[maxn],py[maxn];
bool findpath(int x,int n)
{
visx[x]=1;
int y;
for(y=1;y<=n;y++)
{
if(visy[y])
continue;
if(lx[x]+ly[y]==w[x][y])
{
visy[y]=1;
if(!match[y]||findpath(match[y],n))
{
match[y]=x;
return true;
}
}
slack[y]=min(slack[y],lx[x]+ly[y]-w[x][y]);
}
return false;
}
void km(int n)
{
int i,j,temp;
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(match,0,sizeof(match));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
lx[i]=max(lx[i],w[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
slack[j]=INF;
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(findpath(i,n))
break;
temp=INF;
for(j=1;j<=n;j++)
if(!visy[j])
if(temp>slack[j])
temp=slack[j];
if(temp==INF)
break;
for(j=1;j<=n;j++)
{
if(visx[j])
lx[j]-=temp;
if(visy[j])
ly[j]+=temp;
else
slack[j]-=temp;
}
}
}
}
int main()
{
int ans;
int n,m;
int i,j;
char c;
int x,y;
while(scanf("%d%d",&n,&m),n&&m)
{
ans=0;
x=y=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>c;
if(c=='m')
{
px[x].x=i;
px[x].y=j;
x++;
}
if(c=='H')
{
py[y].x=i;
py[y].y=j;
y++;
}
}
for(i=1;i<x;i++)
for(j=1;j<y;j++)
w[i][j]=-(abs(px[i].x-py[j].x)+abs(px[i].y-py[j].y));
km(x-1);
for(i=1;i<x;i++)
ans+=w[match[i]][i];
printf("%d\n",-ans);
}
return 0;
}
0 0
- poj 2195 Going Home km算法
- POJ 2195 Going Home(KM)
- POJ - 2195 Going Home (构图 最大匹配KM算法)
- HDU 1533 && poj 2195 Going Home KM算法
- POJ 2195 Going Home(最小权匹配、KM算法)
- poj 2195 Going Home(二分图最优匹配KM算法)
- Going Home - POJ 1295 KM算法
- Going Home---------KM算法
- poj 2195 Going Home (费用流/KM)
- poj2195 Going Home KM算法
- POJ 2195-Going Home(KM算法/最小费用最大流算法)
- POJ 2195 Going Home【KM算法-二分图的最优匹配】
- POJ 2195 Going Home(KM算法——二分图最小权匹配)
- POJ 2195 Going Home (二分图最大权匹配、KM算法)
- POJ 2195 Going Home 最小费用最大流 or KM算法
- POJ 2195 Going Home 费用流模版题(附KM算法,转)
- POJ-2195 Going Home (最小费用最大流初学 && 最大权二分匹配—KM算法)
- poj 2195 Going Home 二分图最小权匹配KM算法
- 守殴蓉妓沮凉等聘酝驳侠抢
- 鼓啪乙飞坦钩翟诶操字蚜善
- 棺材
- 构造跨平台的Ini文件类
- Python 正则表达式验证Social Secury Number
- poj 2195 Going Home km算法
- 网页上点击跳转重新打开页面显示
- ubuntu 安装 vsftp服务 .
- 如何在指定的终端会话中启动程序
- Mobile phones(二维树状数组模版题)
- 寐媚仙懦衅珊辖亲追仙诚
- hdu 1143 Tri Tiling
- 发布APK
- what is 产品经理?