POJ_2195_Going Home_最小费用最大流

Going Home
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 20807        Accepted: 10543

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


题意:m个人和m个房子,每个房子中要安排一个人,每个人移动一步费用为1,

    所有人安排房子后需要的金钱的最小值。


曼哈顿距离例如在平面上,坐标(x1, y1)的i点与坐标(x2, y2)的j点的曼哈顿距离为:
      d(i,j)=|X1-X2|+|Y1-Y2|.

建图:网络流,建一个超源 0,超汇  E

    1> 超源  0  到 所有人建一条边  ,容量为  1   费用为  0

    2> 所有人到所有房子  建一条边   容量为1,正向费用为用: abs(x人-x房子)+abs(y人-y房子)。

反向为:   -正向

    3> 所有房子  到  超汇(E) 建一条边 , E = 所有人个数 + 所有边个数 + 1 。容量为 1 ,费用为 0


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> 
#include <queue>
using namespace std;
#define Max 202
#define INF 0x3f3f3f3f 
bool vis[Max];
int dist[Max];
int pre[Max];
int cost[Max][Max];
int cap[Max][Max];
int E, sh, sm, res;

struct Node
{
    int x, y;
}hos[Max], man[Max];

bool spfa()//求最短路 
{
    int i, u;
   	memset(dist,0x3f,sizeof(dist));
	memset(vis,false,sizeof(vis)); 
    queue <int> q;
    dist[0] = 0;
    vis[0] = true;
    while (!q.empty())
        q.pop();
    q.push(0);
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = false;
        for (i = 1; i <= E; i++)
        {
            if (cap[u][i] && dist[i] > dist[u] + cost[u][i])
            {
                dist[i] = dist[u] + cost[u][i];
                pre[i] = u;
                if (!vis[i])
                {
                    vis[i] = true;
                    q.push(i);
                }
            }
        }
    }
    if(dist[E] != INF)
	{ 
        return true;
    }
    return false;
}

void change()//更新 流量 
{
    int i, Min;
    Min = INF;
    for (i = E; i != 0; i = pre[i])
    {
        Min = min(Min, cap[pre[i]][i]);
    }
    for (i = E; i != 0; i = pre[i])
    {
        cap[pre[i]][i] -= Min;
        cap[i][pre[i]] += Min;
        res += cost[pre[i]][i] * Min;
    }
}

int main()
{
    int i, j, n, m;
    while(scanf("%d%d",&n,&m) && (n+m))
    { 
		char c;
        sh = 0; sm = 0;
        memset(cap, 0, sizeof(cap));
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                scanf("\n%c", &c); 
                if (c == 'H')
                {
					sh++;
                    hos[sh].x = i;
                    hos[sh].y = j;    
                }
                else if (c == 'm')
                {
					sm++;
                    man[sm].x = i;
                    man[sm].y = j; 
                }
            }
        }
        E = sh + sm + 1;//超汇 
        
	    for (i = 1; i <= sm; i++) //超源为0   0到 所有人 
	    {
	        cap[0][i] = 1;   //容量为  1 
	        cost[0][i] = 0;  //费用为  0 
	    }
	    for (i = 1; i <= sm; i++)    //每个人到 每个房子 
	    {
	        for (j = 1; j <= sh; j++)
	        {
	            cap[i][sm+j] = 1;
	            cost[i][sm+j] = abs(man[i].x-hos[j].x) + abs(man[i].y-hos[j].y);      //正向边费用 
	            cost[sm+j][i] = -cost[i][sm+j];                                      // 反向边费用 
	        }
	    }
	    for (i = 1; i <= sh; i++) //所有房子 到  E   超汇为E 
	    {
	        cap[sm+i][E] = 1;
	        cost[sm+i][E] = 0;
	    }
	    
        res = 0;
        while(spfa())
        { 
            change();
        } 
        printf("%d\n", res);
    }
    return 0;
}



挑战程序上的模板


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <queue>
#define Max 210
using namespace std;
struct loc
{  
    int x,y;  
}coordinate[Max];  
struct Edge
{  
    int to,cap,cost,rev;  //终点,容量,费用,反向边 
};  
vector<Edge> g[Max];  //图的邻接表 
queue<int> Q;  
char map[Max][Max];  
int dist[Max];   //记录最短路径 
int prevv[Max];  //最短路的前驱点 
int preve[Max];  //前驱点对应的边 
bool vis[Max];  
int n,m;  
int sh,sm;  
int E; 

//向图中增加一条从from到to容量为cap费用为cost的边 
void add_edge(int from,int to,int cap,int cost)  
{  
    g[from].push_back((Edge){to,cap,cost,g[to].size()});  
    g[to].push_back((Edge){from,0,-cost,g[from].size()-1});  
}
int min_cost_flow(int f)//spfa求最短路,并记录路径 
{
	int res=0;  
    int u,v;  
    while(f>0)  
    {  
        memset(dist,0x3f,sizeof(dist));  
        memset(vis,0,sizeof(vis));  
        dist[0]=0;  
        vis[0]=1;  
        Q.push(0);  
        while(!Q.empty())  
        {  
            u=Q.front();Q.pop();vis[u]=0;  
            for(v=0;v<g[u].size();v++)  
            {  
                Edge &e=g[u][v];  
                if(e.cap>0&&dist[e.to]>dist[u]+e.cost)  
                {  
                    dist[e.to]=dist[u]+e.cost;  
                    prevv[e.to]=u;  
                    preve[e.to]=v;  
                    if(!vis[e.to])  
                    {  
                        Q.push(e.to);  
                        vis[e.to]=1;  
                    }  
                }  
            }  
        } 
		f-=1;  
        res+=dist[E]; 
        for(v=E;v!=0;v=prevv[v])    //更改路径流量 
        {  
            Edge &e = g[prevv[v]][preve[v]];  
            e.cap -= 1;
			g[v][e.rev].cap += 1;   
            //g[e.to][e.rev].cap += 1;  
        }    
    }  
    return res;  
} 
int main()
{
	while(scanf("%d%d",&n,&m) && (n+m))
	{
		int i,j,k;
		getchar();
		sh = 0,sm = 0;
		for(i=0;i<Max;i++)
		{
			g[i].clear(); 
		}
		for(i=0;i<n;i++)
		{
			gets(map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j] == 'H')
				{
					sh ++ ;
					coordinate[sh].x = i;
					coordinate[sh].y = j;
					add_edge(0,sh,1,0);//超级源点 连接  每个人
				}
			}
		}	
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j] == 'm')
				{
					sm ++ ;
					for(k=1;k<=sh;k++)
					{
						add_edge(k,sh+sm,1,abs(coordinate[k].x-i)+abs(coordinate[k].y-j));//每一个人连接 每一个房子 
					}
				}
			}
		}
		E = sh+sm+1;
		for(i=sh+1;i<E;i++)
		{
			add_edge(i,E,1,0);//每一个房子连接 超级汇点 
		}
		int f = min(sh,sm);
		printf("%d\n",min_cost_flow(f));
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值