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


    Going Home

    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 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

    最小费用最大流的两个模板

    /* ******************************************************************
    // ******************************************************************
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    //************************************************************
    //最小费用最大流算法
    //SPFA求最短路
    //邻接矩阵形式
    //初始化:cap:容量,没有边为0
    //cost:耗费,对称形式,没有边的也为0
    //c是最小费用
    //f是最大流
    //*******************************************************
    const int MAXN=500;
    const int INF=0x3fffffff;
    int cap[MAXN][MAXN];//容量,没有边为0
    int flow[MAXN][MAXN];
    //耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数
    int cost[MAXN][MAXN];
    
    
    int n;//顶点数目0~n-1
    int f;//最大流
    int c;//最小费用
    int st,ed;//源点和汇点
    
    bool vis[MAXN];//在队列标志
    int que[MAXN];
    int pre[MAXN];
    int dis[MAXN];//s-t路径最小耗费
    bool SPFA()
    {
        int front=0,rear=0;
        for(int u=0;u<=n;u++)
        {
            if(u==st)
            {
                que[rear++]=u;
                dis[u]=0;
                vis[u]=true;
            }
            else
            {
                dis[u]=INF;
                vis[u]=false;
            }
        }
        while(front!=rear)
        {
            int u=que[front++];
            vis[u]=false;
            if(front>=MAXN)front=0;
            for(int v=0;v<=n;v++)
            {
                if(cap[u][v]>flow[u][v]&&dis[v]>dis[u]+cost[u][v])
                {
                    dis[v]=dis[u]+cost[u][v];
                    pre[v]=u;
                    if(!vis[v])
                    {
                        vis[v]=true;
                        que[rear++]=v;
                        if(rear>=MAXN)rear=0;
                    }
                }
            }
        }
        if(dis[ed]>=INF)return false;
        return true;
    }
    
    void minCostMaxflow()
    {
        memset(flow,0,sizeof(flow));
        c=f=0;
        while(SPFA())
        {
            int Min=INF;
            for(int u=ed;u!=st;u=pre[u])
               Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]);
            for(int u=ed;u!=st;u=pre[u])
            {
                flow[pre[u]][u]+=Min;
                flow[u][pre[u]]-=Min;
            }
            c+=dis[ed]*Min;
            f+=Min;
        }
    }
    //************************************************************
    
    struct Node{
        int x,y;
    };
    Node node1[MAXN],node2[MAXN];
    char str[MAXN][MAXN];
    
    int main()
    {
        int N,M;
        while(scanf("%d%d",&N,&M)==2){
            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]=='H'){
                        tol1++;
                        node1[tol1].x=i;
                        node1[tol1].y=j;
                    }
                    if(str[i][j]=='m'){
                        tol2++;
                        node2[tol2].x=i;
                        node2[tol2].y=j;
                    }
                }
            }
            st=0,ed=tol1+tol2+1;
            n=tol1+tol2+1;
            memset(cap,0,sizeof(cap));
            memset(cost,0,sizeof(cost));
            for(int i=1;i<=tol2;i++){
                cost[st][i]=cost[i][st]=0;
                cap[st][i]=1;
            }
            for(int i=1;i<=tol1;i++){
                cost[tol2+i][ed]=0;
                cap[tol2+i][ed]=1;
            }
            for(int i=1;i<=tol2;i++){
                for(int j=1;j<=tol1;j++){
                    cost[i][tol2+j]=abs(node2[i].x-node1[j].x)+abs(node2[i].y-node1[j].y);
                    cost[tol2+j][i]=-cost[i][tol2+j];
                    cap[i][tol2+j]=1;
                }
            }
            minCostMaxflow();
            printf("%d
    ",c);
        }
        return 0;
    }
    
    * ************************************************************************************** */
    
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    //************************************************************
    //最小费用最大流算法
    //SPFA求最短路
    //邻接表形式
    //c是最小费用
    //f是最大流
    //*******************************************************
    const int MAXN=1000;
    const int MAXE=100000;
    const int INF=0x3f3f3f3f;
    struct Edge
    {
        int from;
        int to;
        int next;
        int re;//记录逆边的下标
        int cap;//容量
        int cost;//费用
    }edge[MAXE];
    int head[MAXN],tol;
    int pre[MAXN];
    int vis[MAXN];
    int dis[MAXN];
    int que[MAXN];
    
    void init()
    {
        tol=0;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int cap,int cost)
    {
        edge[tol].from=u;
        edge[tol].to=v;
        edge[tol].cap=cap;
        edge[tol].cost=cost;
        edge[tol].re=tol+1;
        edge[tol].next=head[u];
        head[u]=tol++;
    
        edge[tol].from=v;
        edge[tol].to=u;
        edge[tol].cap=0;
        edge[tol].cost=-cost;
        edge[tol].re=tol-1;
        edge[tol].next=head[v];
        head[v]=tol++;
    }
    
    int n;
    int st;
    int ed;
    bool SPFA()
    {
        int front=0,rear=0;
        for(int v=0;v<=n;v++)
        {
            if(v==st)
            {
                que[rear++]=v;
                vis[v]=true;
                dis[v]=0;
            }
            else
            {
                dis[v]=INF;
                vis[v]=false;
            }
        }
        while(front!=rear)
        {
            int u=que[front++];
            vis[u]=false;
            if(front>=MAXN)front=0;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost)
                {
                    dis[v]=dis[u]+edge[i].cost;
                    pre[v]=i;
                    if(!vis[v])
                    {
                        que[rear++]=v;
                        vis[v]=true;
                        if(rear>=MAXN)rear=0;
                    }
                }
            }
        }
        if(dis[ed]==INF)return false;
        return true;
    }
    int c;//费用
    int f;//最大流
    
    void minCostMaxflow()
    {
        c=f=0;
        int u,p;
        while(SPFA())
        {
            int Min=INF;
            for(u=ed;u!=st;u=edge[p].from)
            {
                p=pre[u];
                Min=min(Min,edge[p].cap);
            }
            for(u=ed;u!=st;u=edge[p].from)
            {
                p=pre[u];
                edge[p].cap-=Min;
                edge[edge[p].re].cap+=Min;
    
            }
            c+=dis[ed]*Min;
            f+=Min;
        }
    }
    
    struct Node{
        int x,y;
    };
    Node node1[MAXN],node2[MAXN];
    char str[MAXN][MAXN];
    
    int main()
    {
        int N,M;
        while(scanf("%d%d",&N,&M)==2){
            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]=='H'){
                        tol1++;
                        node1[tol1].x=i;
                        node1[tol1].y=j;
                    }
                    if(str[i][j]=='m'){
                        tol2++;
                        node2[tol2].x=i;
                        node2[tol2].y=j;
                    }
                }
            }
            st=0,ed=tol1+tol2+1;
            n=tol1+tol2+1;
    
            init();
            for(int i=1;i<=tol2;i++){
                addedge(st,i,1,0);
            }
            for(int i=1;i<=tol1;i++){
                addedge(tol2+i,ed,1,0);
            }
            for(int i=1;i<=tol2;i++){
                for(int j=1;j<=tol1;j++){
                    int tmp=abs(node2[i].x-node1[j].x)+abs(node2[i].y-node1[j].y);
                    addedge(i,tol2+j,1,tmp);
                }
            }
            minCostMaxflow();
            printf("%d
    ",c);
        }
        return 0;
    }
  • 相关阅读:
    git使用
    silverlight与wcf双向通讯 例子
    Oracle 存储过程
    C# 视频教程
    佩服的技术大牛 “赵劼”
    setTimeout setInterval
    js闭包
    MVC Razor视图引擎控件
    MVC json
    springboot创建多环境profile打包
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5620732.html
Copyright © 2020-2023  润新知