现在的位置: 首页 > 综合 > 正文

hdu1533 最小费用流

2012年12月20日 ⁄ 综合 ⁄ 共 3801字 ⁄ 字号 评论关闭

Going Home

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


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
 
虽然慢的点 但还是过了~~~
先广搜出个源点到家的距离,建边流量为一,费用为距离。再加入超级源点,超级汇点~~运行一遍最大流。
貌似用二分匹配代码更少~但主要想费用流~~
代码有点长~~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 99999999
using namespace std;
struct node
{
    int u,v,f,c;
};
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
node e[40000];
int num[120][120];
char map[120][120];
int d[120][120];
int vis[120][120],p[205],preedge[205];
int cc,k,ans_flow,ans_cost;
int cost[250];
int first[205];
int next[40000],n,m;
inline void add_edge(int u,int v,int f,int c)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
bool spfa(int s,int t)
{
    int i;

    for(i=0;i<=t;i++)
        cost[i]=inf;
    memset(p,-1,sizeof(p));
    memset(preedge,-1,sizeof(preedge));
    int inq[205];
    memset(inq,0,sizeof(inq));
    queue<int> q;
    q.push(s);
    cost[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        for(i=first[u];i!=-1;i=next[i])
        {
            if(e[i].f)
            {
                int v=e[i].v;
                if(cost[v]>cost[u]+e[i].c)
                {
                    p[v]=u;
                    preedge[v]=i;
                    cost[v]=cost[u]+e[i].c;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(cost[t]>=inf)
        return false;
    else
        return true;
}
void min_cost_flow(int s,int t)
{
    ans_flow=0;
    ans_cost=0;
    while(spfa(s,t))
    {
        int u=t;

        int mm=inf;
        while(p[u]!=-1)
        {
            mm=min(mm,e[preedge[u]].f);
            u=p[u];
        }
        u=t;
        while(p[u]!=-1)
        {
            e[preedge[u]].f-=mm;
            e[preedge[u]^1].f+=mm;
            u=p[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*cost[t];
        //printf("asd %d\n",spfa(s,t));
    }

}
void bfs(int now)
{
    int x=now/m;
    int y=now%m;
    memset(vis,0,sizeof(vis));
    memset(d,0,sizeof(d));
    vis[x][y]=1;
    queue<int> q;
    q.push(now);
    while(!q.empty())
    {
        int tt=q.front();
        q.pop();
        x=tt/m;
        y=tt%m;
        int i;
        for(i=0;i<4;i++)
        {
            int newx=x+xx[i];
            int newy=y+yy[i];
            if((vis[newx][newy])||(newx<0||newx>=n)||(newy<0||newy>=m))
                continue;
            d[newx][newy]=d[x][y]+1;
            vis[newx][newy]=1;
            q.push(newx*m+newy);
            if(map[newx][newy]=='H')
            {
                add_edge(num[now/m][now%m],num[newx][newy],1,d[newx][newy]);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        int i;
        queue<int> qq;
        cc=0;
        k=1;
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            int j;
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='m')
                {
                    qq.push(i*m+j);
                    num[i][j]=k;
                    add_edge(0,k++,1,0);
                }
                else if(map[i][j]=='H')
                {
                    num[i][j]=k;
                    add_edge(k++,202,1,0);
                }

            }
        }
        while(!qq.empty())
        {
            int u=qq.front();
            qq.pop();
            bfs(u);
        }
        int j;
        /*for(i=0;i<=2;i++)
        {
            for(j=first[i];j!=-1;j=next[j])
            {
                printf(" %d %d %d",e[j].v,e[j].f,e[j].c);
            }
            printf("\n");
        }*/
        min_cost_flow(0,202);
        printf("%d\n",ans_cost);

    }
    return 0;
}

抱歉!评论已关闭.