POJ-2195-Going Home(最小费用最大流/二分图)
来源:互联网 发布:西安软件退税政策 编辑:程序博客网 时间:2024/05/22 14:19
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20717 Accepted: 10502
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
把所有m放到H里,一个H放一个m,问最少移动次数
最小费用最大流解法(邻接表):
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <cmath>using namespace std;#define MAXN 1000#define MAXE 100005#define INF 0x3f3f3f3fstruct point{ int x,y;} node1[MAXN],node2[MAXN];struct node{ int cap,flow,next,v,cost;} edge[MAXE];int index;int head[MAXN];void add_edge(int u, int v, int cap, int cost){ edge[index].v=v; edge[index].cost=cost; edge[index].cap=cap; edge[index].flow=0; edge[index].next=head[u]; head[u]=index++; edge[index].v=u; edge[index].cost=-cost; edge[index].cap=0; edge[index].flow=0; edge[index].next=head[v]; head[v]=index++;}int dist(point a,point b){ return abs(a.x-b.x)+abs(a.y-b.y);}int dis[MAXN],pre[MAXN];bool vis[MAXN];char str[MAXN][MAXN];bool SPFA(int s, int t){ queue<int> q; for(int i=0; i<=t; ++i) { dis[i]=INF; vis[i]=0; pre[i]=-1; } dis[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap>edge[i].flow &&dis[v]>dis[u]+edge[i].cost) { dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(dis[t]==INF)return 0; return 1;}int cost;int MinCost_MaxFlow(int s, int t){ int flow=0; cost=0; while(SPFA(s,t)) { int Min=INF; for(int i=pre[t]; i!=-1; i=pre[ edge[i^1].v ]) { if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; } for(int i=pre[t]; i!=-1; i=pre[ edge[i^1].v ]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } flow+=Min; cost+=dis[t]; } return flow;}int main(){ int N,M; while(scanf("%d%d",&N,&M)) { if(N==0&&M==0)break; int tol1=0,tol2=0; for(int i=0;i<N;i++) { scanf("%s",&str[i]); for(int j=0;j<M;j++) { if(str[i][j]=='m') { tol1++; node1[tol1].x=i; node1[tol1].y=j; } else if(str[i][j]=='H') { tol2++; node2[tol2].x=i; node2[tol2].y=j; } } } int end=tol1+tol2+1; memset(head,-1,sizeof(head)); for(int i=1;i<=tol1;i++) add_edge(0,i,1,0); for(int i=1;i<=tol2;i++) add_edge(tol1+i,end,1,0); for(int i=1;i<=tol1;i++) for(int j=1;j<=tol2;j++) { int temp=abs(node1[i].x-node2[j].x)+abs(node1[i].y-node2[j].y); add_edge(i,tol1+j,1,temp); } MinCost_MaxFlow(0,end); printf("%d\n",cost); } return 0;}
0 0
- POJ-2195-Going Home(最小费用最大流/二分图)
- poj - 2195 - Going Home(最小费用最大流)
- poj 2195 Going Home(最小费用最大流)
- Going Home POJ 2195 (最小费用最大流)
- POJ 2195Going Home(最小费用最大流)
- POJ 2195:Going Home(SPFA最小费用最大流)
- poj 2195 going home(最小费用最大流)
- 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最小费用最大流
- hdu 1533 || poj 2195 Going Home (最小费用最大流)
- POJ 2195 Going Home 最小费用最大流
- POJ 2195 Going Home 最小费用最大流
- POJ 2195 Going Home (最小费用最大流)
- 【最小费用最大流】POJ-2195 Going Home
- HTTP请求中POST与GET的区别
- CoreData
- ReactiveCocoa
- c# Linq Lamda表达式使用 GroupBy 分组
- PSR-2 代码风格指南
- POJ-2195-Going Home(最小费用最大流/二分图)
- 深入理解javascript原型和闭包系列 深入理解javascript原型和闭包(12)——简介【作用域】
- Android的文件系统结构 .
- uva 216 Getting in Line
- JAVA入门的文章很多,但是却没有一篇是先宏观介绍JAVA的
- unity再战PBR材质流程与材质制作实践
- 身份证验证
- Java多线程学习(总结)
- Xshell Solarized Dark主题