Poj 2195 Going Home【费用流Min_Cost_Max_flow】
2016-08-09 21:47
453 查看
Going Home
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
Sample Output
Source
Pacific Northwest 2004
题目大意:有m和H,每一个H只能居住一个m,问将所有m分配H,如何分配m去H,使得费用最小。
思路:
1、费用流解最小权匹配问题。
2、建图:
①建立源点S连入各个m,其流量为1,费用为0,表示每个m代表一个人。
②建立源点T,将各个H连入T,其流量为1,费用为0,表示每个H只能居住一个人。
③将每一个m连入各个H,其流量为1,费用为两点之间的曼哈顿距离,表示这个m到这个H的费用。
3、建好图之后直接跑一遍连续增广路即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
struct node2
{
int from;
int to;
int w;
int f;
int next;
int num;
}e[10000000];
struct node
{
int x,y;
}m[150*150],h[150*150];
int head[150*150];
int dis[150*150];
int vis[150*150];
int pre[150*150];
int path[150*150];
int contm,conth,ss,tt,cont;
char a[150][150];
void add(int from,int to,int w,int f)
{
e[cont].from=from;
e[cont].to=to;
e[cont].w=w;
e[cont].f=f;
e[cont].num=cont;
e[cont].next=head[from];
head[from]=cont++;
}
void getmap()
{
cont=0;
ss=conth+contm;
tt=ss+1;
memset(head,-1,sizeof(head));
for(int i=0;i<contm;i++)
{
add(ss,i,0,1);
add(i,ss,0,0);
}
for(int i=0;i<conth;i++)
{
add(i+contm,tt,0,1);
add(tt,i+contm,0,0);
}
for(int i=0;i<contm;i++)
{
for(int j=0;j<conth;j++)
{
int u=i;
int v=j+contm;
add(u,v,abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y),1);
add(v,u,-(abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y)),0);
}
}
}
int SPFA()
{
memset(vis,0,sizeof(vis));
memset(path,-1,sizeof(path));
memset(pre,-1,sizeof(pre));
for(int i=0;i<=tt;i++)dis[i]=0x3f3f3f3f;
vis[ss]=1;
dis[ss]=0;
queue<int >s;
s.push(ss);
while(!s.empty())
{
int u=s.front();
s.pop();vis[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
int f=e[i].f;
if(f&&dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pre[v]=u;
path[v]=e[i].num;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
if(dis[tt]!=0x3f3f3f3f)return 1;
else return 0;
}
void Min_costflow()
{
int maxflow=0;
int ans=0;
while(SPFA()==1)
{
int minn=0x3f3f3f;
for(int i=tt;i!=ss;i=pre[i])
{
minn=min(minn,e[path[i]].f);
}
for(int i=tt;i!=ss;i=pre[i])
{
e[path[i]].f-=minn;
e[path[i]^1].f+=minn;
}
ans+=minn*dis[tt];
maxflow+=minn;
}
printf("%d\n",ans);
}
int main()
{
int r,l;
while(~scanf("%d%d",&r,&l))
{
if(r==0&&l==0)break;
contm=0;
conth=0;
for(int i=0;i<r;i++)
{
scanf("%s",a[i]);
for(int j=0;j<l;j++)
{
if(a[i][j]=='H')
{
h[conth].x=i;
h[conth++].y=j;
}
if(a[i][j]=='m')
{
m[contm].x=i;
m[contm++].y=j;
}
}
}
getmap();
Min_costflow();
}
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21528 | Accepted: 10870 |
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
Pacific Northwest 2004
题目大意:有m和H,每一个H只能居住一个m,问将所有m分配H,如何分配m去H,使得费用最小。
思路:
1、费用流解最小权匹配问题。
2、建图:
①建立源点S连入各个m,其流量为1,费用为0,表示每个m代表一个人。
②建立源点T,将各个H连入T,其流量为1,费用为0,表示每个H只能居住一个人。
③将每一个m连入各个H,其流量为1,费用为两点之间的曼哈顿距离,表示这个m到这个H的费用。
3、建好图之后直接跑一遍连续增广路即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
struct node2
{
int from;
int to;
int w;
int f;
int next;
int num;
}e[10000000];
struct node
{
int x,y;
}m[150*150],h[150*150];
int head[150*150];
int dis[150*150];
int vis[150*150];
int pre[150*150];
int path[150*150];
int contm,conth,ss,tt,cont;
char a[150][150];
void add(int from,int to,int w,int f)
{
e[cont].from=from;
e[cont].to=to;
e[cont].w=w;
e[cont].f=f;
e[cont].num=cont;
e[cont].next=head[from];
head[from]=cont++;
}
void getmap()
{
cont=0;
ss=conth+contm;
tt=ss+1;
memset(head,-1,sizeof(head));
for(int i=0;i<contm;i++)
{
add(ss,i,0,1);
add(i,ss,0,0);
}
for(int i=0;i<conth;i++)
{
add(i+contm,tt,0,1);
add(tt,i+contm,0,0);
}
for(int i=0;i<contm;i++)
{
for(int j=0;j<conth;j++)
{
int u=i;
int v=j+contm;
add(u,v,abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y),1);
add(v,u,-(abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y)),0);
}
}
}
int SPFA()
{
memset(vis,0,sizeof(vis));
memset(path,-1,sizeof(path));
memset(pre,-1,sizeof(pre));
for(int i=0;i<=tt;i++)dis[i]=0x3f3f3f3f;
vis[ss]=1;
dis[ss]=0;
queue<int >s;
s.push(ss);
while(!s.empty())
{
int u=s.front();
s.pop();vis[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
int f=e[i].f;
if(f&&dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pre[v]=u;
path[v]=e[i].num;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
if(dis[tt]!=0x3f3f3f3f)return 1;
else return 0;
}
void Min_costflow()
{
int maxflow=0;
int ans=0;
while(SPFA()==1)
{
int minn=0x3f3f3f;
for(int i=tt;i!=ss;i=pre[i])
{
minn=min(minn,e[path[i]].f);
}
for(int i=tt;i!=ss;i=pre[i])
{
e[path[i]].f-=minn;
e[path[i]^1].f+=minn;
}
ans+=minn*dis[tt];
maxflow+=minn;
}
printf("%d\n",ans);
}
int main()
{
int r,l;
while(~scanf("%d%d",&r,&l))
{
if(r==0&&l==0)break;
contm=0;
conth=0;
for(int i=0;i<r;i++)
{
scanf("%s",a[i]);
for(int j=0;j<l;j++)
{
if(a[i][j]=='H')
{
h[conth].x=i;
h[conth++].y=j;
}
if(a[i][j]=='m')
{
m[contm].x=i;
m[contm++].y=j;
}
}
}
getmap();
Min_costflow();
}
}
相关文章推荐
- Poj 2516 Minimum Cost【费用流Min_Cost_Max_flow】
- POJ 2195 min_cost_max_flow
- Poj 2135 Farm Tour【Min_Cost_Max_flow】
- hrbust 1716 双狙人【费用流Min_Cost_Max_flow】
- poj2195 Going Home 费用流
- poj-2195-Going Home(最小费用最大流模板题)
- hdu 2686 Matrix【费用流Max_Cost_Max_flow】
- POJ 2195 Going Home [二分图带权匹配] [费用流]
- POJ 2195 Going Home 费用流
- POJ - 2195 Going Home (费用流 水)
- poj - 2195 Going Home (费用流 || 最佳匹配)
- poj 2195 Going Home (费用流/KM)
- POJ 2195 Going Home 费用流 最小费用流
- POJ 2195 Going Home(费用流)
- MincostMaxflow
- poj2195 Going Home 费用流
- Poj 2195 Going Home(费用流)
- POJ 2195 Going Home(网络流-费用流)
- POJ 2195 Going Home(费用流)
- POJ 2195 Going Home(网络流-费用流)