POJ 2195 Going Home (最小费用最大流)

题目链接:http://poj.org/problem?id=2195

题意:n*m的矩阵,地图上有若干个人(m)和房子(H),且人与房子的数量一致。man每移动一格费用为1,一个房子只能住一个人。现在要求所有的人出发,都入住房子,求最少话费。

思路:建立一个超级源点和汇点,源点与人相连费用为0,容量为1,人与房子相连,费用为人与房子的距离,容量为1,房子与汇点相连,费用为0,容量为1

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
const int maxn = 3010;
const int maxm = 80000;
const int inf = 1e8;
#define MIN INT_MIN
#define MAX 1e6
#define LL long long
#define init(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(int i = a;i<b;i++)
#define max(a,b) (a>b)?(a):(b)
#define min(a,b) (a>b)?(b):(a)
using namespace std;
struct node{
    int u,v,w,cap,next;
}edge[maxm];
struct point
{
    int x,y;
}M[110],H[110];
int head[maxn],dis[maxn],pre[maxn];
int cnt,n,Mnum,Hnum;
bool vis[maxn];
void add(int u,int v,int w,int cap)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].cap = cap;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].w = -w;
    edge[cnt].cap = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
void initt()
{
    cnt = 0;
    memset(head,-1,sizeof(head));
    Mnum = 1;
    Hnum = 1;
}
bool SPFA(int s,int t)
{
    queue<int>q;
    while(q.empty()==false) q.pop();

    q.push(s);

    init(vis);
    memset(pre,-1,sizeof(pre));

    FOR(i,s,t+1)
    dis[i] = inf;
    vis[s] = 1;
    dis[s] = 0;

    while(!q.empty())
    {
        int uu = q.front();
        q.pop();
        vis[uu] = 0;
        for(int i = head[uu];i!=-1;i = edge[i].next)
        {
            if(edge[i].cap && dis[edge[i].v] > dis[uu] + edge[i].w)//找最小费用
            {
                dis[edge[i].v] = dis[uu] + edge[i].w;
                pre[edge[i].v] = i;//记录路径
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v] = 1;

                q.push(edge[i].v);
                }

            }
        }
    }
    if(dis[t]!=inf)
        return 1;
    return 0;

}
int MinCostMaxFlow(int s,int t)
{
    int flow = 0,cost = 0;//总流量、总费用
    while(SPFA(s,t))
    {
        int df = inf;
        for(int i = pre[t];i!=-1;i=pre[edge[i].u])
        {
            if(df > edge[i].cap)
                df = edge[i].cap;
        }
        flow += df;//这条路径的流量
        for(int i = pre[t];i!=-1;i=pre[edge[i].u])//更新流量
        {
            edge[i].cap -= df;
            edge[i^1].cap += df;
        }
        cost += df*dis[t];//单位费用诚意流量
    }
    return cost;
}
int main()
{
    int m,s,t;
    char ma[110][110];
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0) break;
        initt();
        FOR(i,0,n)
        {
            scanf("%*c%s",ma[i]);
            FOR(j,0,m)
            {
                if(ma[i][j]=='m')
                {
                    M[Mnum].x = i;
                    M[Mnum++].y = j;
                }
                else if(ma[i][j]=='H')
                {
                    H[Hnum].x = i;
                    H[Hnum++].y = j;
                }
            }
        }
        Mnum--,Hnum--;
        s = 0;
        t = Mnum + Hnum + 1;
       // printf("Mnum = %d  Hnum  =  %d   t = %d\n",Mnum,Hnum,t);

        FOR(i,1,Mnum+1)
        {
            add(s,i,0,1);
            FOR(j,1,Hnum+1)
            {
                int dd = abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y);
               // printf("dd = %d\n",dd);
                add(i,Mnum+j,dd,1);
                //printf("i->j %d->%d = %d\n",i,Mnum+j,dd);
                //add(Mnum+j,t,0,1);
            }
        }
        FOR(j,1,Hnum+1)
       {
           add(Mnum+j,t,0,1);
               // printf("j->t = %d->%d\n",Mnum+j,t);
       }
       int ans =  MinCostMaxFlow(s,t);
       cout<<ans<<endl;
    }
    return 0;
}

POJ 2195 Going Home (最小费用最大流),布布扣,bubuko.com

时间: 2024-08-06 19:46:11

POJ 2195 Going Home (最小费用最大流)的相关文章

POJ 2195 地图的最小费用最大流

思路:这题刚开始看就知道是最小费用最大流了,因为求出最优嘛,而且要m,H要一一对应,所以不是二分图匹配就是最小费用最大流. 不过,刚开始还在想每个m与H之间的最小花费如何求,难道要用dfs搜索吗?这样想之后看了下题目给的时间是1000ms,然后就把dfs搜索m与H之间的最短距离排除了.然后想了想,其实尼玛太简单了,因为题目说了只能垂直与竖直的走,所以最短距离不就是两个横坐标相减与两个纵坐标相减之和嘛! 然后每对m与H之间都连边,流量为1(因为每对匹配不能重复),费用为它们之间的距离即花费:然后建

POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K 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

POJ 2195 Going Home 最小费用最大流 尼玛,心累

D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2195 Appoint description: Description On a grid map there are n little men and n houses. In each unit time, every little man can mo

POJ 2516 Minimum Cost(最小费用最大流,坑题)

题目链接:http://poj.org/problem?id=2516 题意:有N个店,M个供货商,K种商品.已知供货商的仓库里每种商品的数量以及每种商品运送到每个店的费用,每个店铺对各种商品的需求数量,求最少话费. Input  第一行:N,M,K. 然后1 - N行,每行 K列 ,第I行第J个数代表 第I个店铺 需要第J种物品多少件. 然后 N+1 - M行  ,每行 K列 , 第I行第J个数代表 第I个供货商 有第J种物品多少件. 然后是K个矩阵  ,每个N行M列,第ji个矩阵的第i行第j

POJ 2516 Minimum Cost (最小费用最大流)

Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K       Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy

POJ 3680: Intervals【最小费用最大流】

题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由于要最大,所以是求最大费用最大流),容量为1,至于不超过K的限制,只要从源点到第一个点的流量为K就行,剩下每个相邻的点相连,费用为0,流量只要大于的等于K就可以(我取的正无穷) //poj3680 #include <stdio.h> #include <iostream> #incl

POJ 2135 Farm Tour [最小费用最大流]

题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很难,因为要保证每条边只能走一次,那么我们把边拆为两个点,一个起点和终点,容量是1,权重是这条路的长度.然后两个端点分别向起点连接容量是1权重是0的边,终点分别向两个端点连容量是1权重是0的边,从源点到1连容量为2权重为0的边,从n到汇点连容量为2权重为0的边. #include<stdio.h>

POJ 2195:Going Home(最小费用最大流)

http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大流.最小费用最大流是保证在流量最大的情况下,使得费用最小. 建图是把S->人->家->T这些边弄上形成一个网络,边的容量是1(因为一个人只能和一个家匹配),边的费用是曼哈顿距离,反向边的费用是-cost. 算法的思想大概是通过SPFA找增广路径,并且找的时候费用是可以松弛的.当找到这样一条增

poj 2195 最小费用最大流模板

/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accepted Source Code */ #include <iostream> #include <stdio.h> #include <queue> #include <math.h> #include <string.h> using namespa