• poj-2195 Going Home(费用流)


    Going Home
    Time Limit: 1000MS
    Memory Limit: 65536K
    Total Submissions: 24304
    Accepted: 12198

    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<map>
    #include<set>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    #define ll long long
    #define maxn 10010
    #define maxm 400100
    using namespace std;
    struct Edge{
        Edge(){};
        Edge(int a,int b,int c,int d){v=a;f=b;w=c;nxt=d;};
        int v,f,w,nxt;
    };
    struct space{
        int x,y;
    }home[maxn],man[maxn];
    int n,lmt;
    int g[maxn+10];
    char mmp[maxn][maxn];
    Edge e[maxm+10];
    int nume;
    int src,sink;
    void addedge(int u,int v,int c,int w){
        e[++nume]=Edge(v,c,w,g[u]);
        g[u]=nume;
        e[++nume]=Edge(u,0,-w,g[v]);
        g[v]=nume;
    }
    queue<int>que;
    bool inQue[maxn+10];
    int dist[maxn+10];
    int prev[maxn+10],pree[maxn+10];
    bool findpath(){
        while(!que.empty())que.pop();
        que.push(src);memset(dist,inf,sizeof(dist));
        dist[src]=0;inQue[src]=true;
        while(!que.empty()){
            int u=que.front();
            que.pop();
            for(int i=g[u];i;i=e[i].nxt){
                if(e[i].f>0&&dist[u]+e[i].w<dist[e[i].v]){
                    dist[e[i].v]=dist[u]+e[i].w;
                    prev[e[i].v]=u;pree[e[i].v]=i;
                    if(!inQue[e[i].v]){
                        inQue[e[i].v]=true;que.push(e[i].v);
                    }
                }inQue[u]=false;
            }
        }
        if(dist[sink]<inf)return true;else return false;
    }
    int augement(){
        int u=sink;int delta=inf;
        while(u!=src){
            if(e[pree[u]].f<delta)delta=e[pree[u]].f;
            u=prev[u];
        }u=sink;
        while(u!=src){
            e[pree[u]].f-=delta;
            e[pree[u]^1].f+=delta;
            u=prev[u];
        }
        return dist[sink]*delta;
    }
    int mincostflow(){
        int cur=0,ans=0;
        while(findpath()){
            cur+=augement();}
        return cur;
    }
    int main(){
        while(~scanf("%d%d",&n,&lmt)&&(lmt|n)){nume=1;memset(g,0,sizeof(g));
            src=0;
            for(int i=0;i<n;i++){cin>>mmp[i];}int l=0,k=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<lmt;j++){
                    if(mmp[i][j]=='H'){home[l].x=i+1;home[l].y=j+1;l++;}
                    if(mmp[i][j]=='m'){man[k].x=i+1;man[k].y=j+1;k++;}
                }
            }sink=k+l+1;
            for(int i=0;i<k;i++){addedge(0,i+1,1,0);}
            for(int i=0;i<l;i++){addedge(k+i+1,sink,1,0);}
            for(int i=0;i<k;i++){
                for(int j=0;j<l;j++){
                    int lk=abs(man[i].x-home[j].x)+abs(man[i].y-home[j].y);
                    addedge(i+1,k+j+1,1,lk);
                }
            }
            cout<<mincostflow()<<endl;
        }
    }
    

  • 相关阅读:
    kerberos和hadoop的集成
    kerberos的原理
    Impala与Hive的异同
    为什么hdfs不适合存储小文件
    猛犸数据质量
    CIO的发展之路
    企业数据能力测评:认清现状,布局未来
    企业架构的演变
    其实,他只是个没长大的孩子【复制知乎
    看着自己之前写的便签,想笑
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053262.html
Copyright © 2020-2023  润新知