HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)
来源:互联网 发布:python 爬取搜索引擎 编辑:程序博客网 时间:2024/05/22 05:01
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.
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.
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
2 2.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
21028
一开始看到这道题就感觉是二分匹配不过最近在学网络流好像也可以写
二分图最小权匹配
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>b#include<algorithm>#include<map>#include<vector>#include<queue>#include<stack>#define eps 1e-8const int INF = 0x3f3f3f3f;const int N=105;using namespace std;int n,m,o;struct node{ int x,y;}xx[N],yy[N];char mapp[N][N];int mp[N][N];int b1[N],b2[N],v1[N],v2[N],p[N],s[N];int dfs(int u){ v1[u]=1; for(int i=1;i<=o;i++) { if(v2[i]) continue; int gap=b1[u]+b2[i]-mp[u][i]; if(!gap) { v2[i]=1; if(p[i]==-1||dfs(p[i])) { p[i]=u; return 1; } } else s[i]=min(gap,s[i]); } return 0;}int km(){ memset(p,-1,sizeof(p)); memset(b2,0,sizeof(b2)); for(int i=1;i<=o;i++) { b1[i]=-INF; for(int j=1;j<=o;j++) b1[i]=max(b1[i],mp[i][j]); } for(int i=1;i<=o;i++) { memset(s,INF,sizeof(s)); while(1) { memset(v1,0,sizeof(v1)); memset(v2,0,sizeof(v2)); if(dfs(i)) break; int d=INF; for(int j=1;j<=o;j++) if(!v2[j]) d=min(d,s[j]); for(int j=1;j<=o;j++) { if(v1[j]) b1[j]-=d; if(v2[j]) b2[j]+=d; else s[j]-=d; } } } int res=0; for(int i=1;i<=o;i++) res+=mp[p[i]][i]; return res;}int main(){ while(~scanf("%d%d",&n,&m)&&(n+m)) { int o1=0,o2=0; for(int i=0;i<n;i++) { scanf("%s",&mapp[i]); for(int j=0;j<m;j++) { if(mapp[i][j]=='m') { xx[++o1].x=i; xx[o1].y=j; } if(mapp[i][j]=='H') { yy[++o2].x=i; yy[o2].y=j; } } } o=o1; memset(mp,-INF,sizeof(mp)); for(int i=1;i<=o;i++) for(int j=1;j<=o;j++) mp[i][j]=-abs(xx[i].x-yy[j].x)-abs(xx[i].y-yy[j].y); printf("%d\n",-km()); }}最小费用最大流
PS
这是一个最小费用最大流问题
//最大费用最小流只要在添加边的时候换一下位置就好了
//求最大费用最大流只需要把费用换成相反数,用最小费用最大流求解即可
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<queue>#include<stack>#define eps 1e-8#define ll long longconst int inf = 0x3f3f3f3f;const long long mod=1e9+7;const int nMax=1105;using namespace std;char s[nMax][nMax];int cost[nMax][nMax];int cap[nMax][nMax];int v[nMax];int d[nMax];queue<int>q;int pre[nMax];int t,ans,cnt;struct node{ int x,y;}a1[nMax],a2[nMax];void end(){ for(int i=t;i!=0;i=pre[i]) { cap[pre[i]][i]-=1; cap[i][pre[i]]+=1; } ans+=d[t];}int spfa(){ memset(v,0,sizeof(v)); memset(d,inf,sizeof(d)); d[0]=0; v[0]=1; q.push(0); while(!q.empty()) { int k=q.front(); q.pop(); v[k]=0; for(int i=0;i<=t;i++) { if(cap[k][i]&&d[i]>d[k]+cost[k][i]) { d[i]=d[k]+cost[k][i]; pre[i]=k; if(!v[i]) { v[i]=1; q.push(i); } } } } if(d[t]==inf) return 0; return 1;}int main(){ int n,m,cnt1,cnt2; while(~scanf("%d%d",&n,&m)&&n+m) { memset(cap,0,sizeof(cap)); memset(cost,0,sizeof(cost)); cnt1=cnt2=ans=0; for(int i=0;i<n;i++) { scanf("%s",&s[i]); for(int j=0;j<m;j++) { if(s[i][j]=='H') a1[++cnt1].x=i,a1[cnt1].y=j; if(s[i][j]=='m') a2[++cnt2].x=i,a2[cnt2].y=j; } } cnt=cnt1; t=2*cnt+1; for(int i=1;i<=cnt;i++) { cap[0][i]=1; cap[i+cnt][t]=1; for(int j=1;j<=cnt;j++) { cap[i][j+cnt]=1; cost[i][j+cnt]=abs(a1[i].x-a2[j].x)+abs(a1[i].y-a2[j].y); cost[j+cnt][i]-=cost[i][j+cnt]; } } while(spfa()) end(); printf("%d\n",ans); }}
阅读全文
0 0
- HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)
- HDU 1533 Going Home 最小费用最大流(入门)(模板)或者 二分匹配
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
- POJ-2195 Going Home (最小费用最大流初学 && 最大权二分匹配—KM算法)
- POJ 2195&&HDU 1533 Going Home(KM算法解决二分图最小权匹配)
- HDU 1533 Going Home【最小费用流|KM算法】
- HDU-1533 KM Going Home 最佳匹配 最小费用最大流
- HDU 1533 Going Home (二分图最小权匹配 KM模板)
- hdu 1533 Going Home(最小权匹配KM)
- hdu 1533 Going Home(KM 最小权值匹配)
- hdoj 2255 奔小康赚大钱 (KM算法 详解+模板) && HDU 1533 Going Home (二分图最小权匹配 KM模板)纯模板
- HDU 2426 Interesting Housing Problem 二分匹配(KM模板)或者最小费用最大流
- poj 2195 Going Home--最小费用最大流--spfa--动态数组--或者用 最小权匹配
- hdu 1533 going home (最小费用最大流)
- POJ 2195-Going Home(KM算法/最小费用最大流算法)
- hdu1533 Going Home KM算法求最小费用
- hdu 1533 Going Home 【最小权的二分图匹配】
- POJ 2195 Going Home(最小权匹配、KM算法)
- NOIP2017 游记 Day2
- 常良项目opencv调试
- js实现对上传图片进行压缩并且预览
- HDU2057 A + B Again【水题】
- 用函数判断多组数据是否是水仙花数
- HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)
- jzoj 4883. 【NOIP2016提高A组集训第12场11.10】灵知的太阳信仰
- SpringBoot开发微信公众号(五)
- MAC自带的SVN的服务器端配置
- SPARK的线性代数库BLAS
- 文章标题
- 【剑指offer】数组中重复的数字
- angular综合题 添加 删除 排序 隔行变色 等
- (十五)java并发编程--线程的死锁(deadlock)