HDU 1533 Going Home 最小费用最大流(入门)(模板)或者 二分匹配

点击打开链接

 

Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2133    Accepted Submission(s): 1063


Problem 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
 


 

Source
 


 

Recommend
lwg

 

 

第一次做最小费用最大流,此题虐我一天。不过功夫不负有心人,终于找到一个不错的模板,以后不愁了。当然此题用二分匹配做更容易一些,用KM算法的话求的是最大匹配,而此题 要求的是一个最小匹配,所以你可以一开始赋初值的时候全部取其负值,结果输出在取其负值。

 最小费用最大流算法:

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int sumFlow;
const int MAXN = 1010;
const int MAXM = 1000200;
const int INF = 1000000000;
struct node
{
    int x,y;
}H[300],M[300];
struct Edge
{
    int u, v, cap, cost;
    int next;
}edge[MAXM<<2];
int NE;
int head[MAXN], dist[MAXN], pp[MAXN];
bool vis[MAXN];
char map[MAXN][MAXN];
void init()
{
    NE = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
    edge[NE].u = u; edge[NE].v = v; edge[NE].cap = cap; edge[NE].cost = cost;
    edge[NE].next = head[u]; head[u] = NE++;
    edge[NE].u = v; edge[NE].v = u; edge[NE].cap = 0; edge[NE].cost = -cost;
    edge[NE].next = head[v]; head[v] = NE++;
}
bool SPFA(int s, int t, int n)
{
    int i, u, v;
    queue <int> qu;
    memset(vis,false,sizeof(vis));
    memset(pp,-1,sizeof(pp));
    for(i = 0; i <= n; i++) dist[i] = INF;
    vis[s] = true; dist[s] = 0;
    qu.push(s);
    while(!qu.empty())
    {
        u = qu.front(); qu.pop(); vis[u] = false;
        for(i = head[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].v;
            if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
            {
                dist[v] = dist[u] + edge[i].cost;
                pp[v] = i;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    if(dist[t] == INF) return false;
    return true;
}
int MCMF(int s, int t, int n) // minCostMaxFlow
{
    int flow = 0; // 总流量
    int i, minflow, mincost;
    mincost = 0;
    while(SPFA(s, t, n))
    {
        minflow = INF + 1;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
            if(edge[i].cap < minflow)
                minflow = edge[i].cap;
        flow += minflow;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
        {
            edge[i].cap -= minflow;
            edge[i^1].cap += minflow;
        }
        mincost += dist[t] * minflow;
    }
    sumFlow = flow; // 最大流
    return mincost;
}
int main()
{
    int n, m;
    int u, v, c;
    while (scanf("%d%d", &n, &m),n|m)
    {
        init();
        int ch=0,cm=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='H')
                {
                    H[ch].x=i;
                    H[ch++].y=j;
                }
                else if(map[i][j]=='m')
                {
                    M[cm].x=i;
                    M[cm++].y=j;
                }
            }
        }
        int S = 0;
        int T = 2*ch+1;
        for(int i=0;i<cm;i++)
        {
            addedge(0,i+1,1,0);
            for(int j=0;j<ch;j++)
            {
                int temp=fabs(H[i].x-M[j].x)+fabs(H[i].y-M[j].y);
                addedge(i+1,j+1+ch,1,temp);
            }
			addedge(i+1+ch,T,1,0);
        }
        int ans = MCMF(S, T, T+1);
        printf("%d\n", ans);
    }
    return 0;
}


 

 

 

以下是模板,方便以后用:

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int sumFlow;
const int MAXN = 1010;
const int MAXM = 1000200;
const int INF = 1000000000;
struct Edge
{
    int u, v, cap, cost;
    int next;
}edge[MAXM<<2];
int NE;
int head[MAXN], dist[MAXN], pp[MAXN];
bool vis[MAXN];
void init()
{
    NE = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
    edge[NE].u = u; edge[NE].v = v; edge[NE].cap = cap; edge[NE].cost = cost;
    edge[NE].next = head[u]; head[u] = NE++;
    edge[NE].u = v; edge[NE].v = u; edge[NE].cap = 0; edge[NE].cost = -cost;
    edge[NE].next = head[v]; head[v] = NE++;
}
bool SPFA(int s, int t, int n)
{
    int i, u, v;
    queue <int> qu;
    memset(vis,false,sizeof(vis));
    memset(pp,-1,sizeof(pp));
    for(i = 0; i <= n; i++) dist[i] = INF;
    vis[s] = true; dist[s] = 0;
    qu.push(s);
    while(!qu.empty())
    {
        u = qu.front(); qu.pop(); vis[u] = false;
        for(i = head[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].v;
            if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
            {
                dist[v] = dist[u] + edge[i].cost;
                pp[v] = i;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    if(dist[t] == INF) return false;
    return true;
}
int MCMF(int s, int t, int n) // minCostMaxFlow
{
    int flow = 0; // 总流量
    int i, minflow, mincost;
    mincost = 0;
    while(SPFA(s, t, n))
    {
        minflow = INF + 1;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
            if(edge[i].cap < minflow)
                minflow = edge[i].cap;
        flow += minflow;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
        {
            edge[i].cap -= minflow;
            edge[i^1].cap += minflow;
        }
        mincost += dist[t] * minflow;
    }
    sumFlow = flow; // 最大流
    return mincost;
}
int main()
{
    int n, m;
    int u, v, c;
    init();
        
    ......//建图    
    int S;//S是源点
    int T ;//T是汇点
    
    int ans = MCMF(S, T, T+1);//T+1是点的个数
    printf("%d\n", ans);
    
    return 0;
}


 二分匹配(KM算法):

//二分匹配
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 99999
using namespace std;
int g[507][507];
int lx[507],ly[507];
int slack[507],match[507];
bool visx[507],visy[507];
int n,m,t,cm,ch;
char map[507][507];
struct node
{
    int x,y;
}M[507],H[507];
bool dfs(int cur)
{
    visx[cur]=true;
    for(int y=1;y<=m;y++)
    {
        if(visy[y])continue;
        int t=lx[cur]+ly[y]-g[cur][y];
        if(t==0)
        {
            visy[y]=true;
            if(match[y]==-1||dfs(match[y]))
            {
                match[y]=cur;
                return true;
            }
        }
        else if(slack[y]>t)
        {
            slack[y]=t;
        }
    }
    return false;
}

int KM()
{
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        lx[i]=-inf;
        for(int j=1;j<=m;j++)
        {
            if(g[i][j]>lx[i])
            lx[i]=g[i][j];
        }
    }
    for(int x=1;x<=n;x++)
    {
        for(int i=1;i<=m;i++)
        slack[i]=inf;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))break;
            int d=inf;
            for(int i=1;i<=m;i++)
            {
                if(!visy[i]&&d>slack[i])
                d=slack[i];
            }
            for(int i=1;i<=n;i++)
            if(visx[i])
            {
                lx[i]-=d;
            }
            for(int i=1;i<=m;i++)
            if(visy[i])ly[i]+=d;
            else slack[i]-=d;
        }
    }
    int reslut=0,flag=0;
    for(int i=1;i<=m;i++)
    {
        if(match[i]==-1||g[match[i]][i]==-inf)
        continue;
        if(match[i]>-1)
        {
            reslut+=g[match[i]][i];
            flag++;
        }
    }
    if(flag<n)reslut=-1;
    return reslut;
}
int main()
{
    while(scanf("%d%d",&n,&m),n|m)
    {
        cm=0;ch=0;
        memset(g,0,sizeof(g));
        for(int i=0;i<n;i++)
        scanf("%s",map[i]);
        memset(g,-inf,sizeof(g));
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            if(map[i][j]=='H')
                {
                    H[ch].x=i;
                    H[ch++].y=j;
                }
                else if(map[i][j]=='m')
                {
                    M[cm].x=i;
                    M[cm++].y=j;
                }
        }
        for(int i=0;i<cm;i++)
        for(int j=0;j<ch;j++)
        {
            int temp=fabs(H[i].x-M[j].x)+fabs(H[i].y-M[j].y);
            int xx=i+1,yy=j+1;
            g[xx][yy]=-temp;
        }
        n=cm,m=ch;
        int ans=KM();
        printf("%d\n",-ans);
    }
    return 0;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值