HDU 1533(最小费用流)

在风神的博客里这道题是费用流的入门题,给风神跪了。。。

个人觉得这道题目挺好的,作为入门题还是有点难的吧? 这道题的做法很多,我是直接裸的跑费用流,跑了八百多毫秒。。。么么哒

题意: 我都看得懂,这就不用说了。。。。
题解: 建立源点,汇点,源点往小矮人连一条流量为1,费用为0的边,每个房间往汇点连一条流量为1,费用为0的边。然后每个点往其右和下一个点建一条流量为INF,费用为1的无向边,表示每一个点可以走过INF个小矮人,每个小矮人走一步花费1刀,这样图就建成了。

/* *********************************************** Author :xdlove Created Time :2015年08月18日 星期二 13时18分54秒 File Name :xd.cpp ************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/******************************** please don't hack me!! /(ToT)/~~ __------__ /~ ~\ | //^\\//^\| /~~\ || T| |T|:~\ | |6 ||___|_|_||:| \__. / o \/' | ( O ) /~~~~\ `\ \ / | |~~\ | ) ~------~`\ /' | | | / ____ /~~~)\ (_/' | | | /' | ( | | | | \ / __)/ \ \ \ \ \/ /' \ `\ \ \|\ / | |\___| \ | \____/ | | /^~> \ _/ < | | \ \ | | \ \ \ -^-\ \ | ) `\_______/^\______/ ************************************/

#define clr(a) memset(a,0,sizeof(a));
typedef long long ll;

const int MAXN = 100 * 100 + 50;
const int MAXM = MAXN * 10;
const int INF = 0x3f3f3f3f;

struct DoubleQueue
{
    int l,r,q[MAXN];
    DoubleQueue()
    {
        l = r = 0;
    }
    bool empty()
    {
        return l == r;
    }
    void push_back(int v)
    {
        q[r++] = v;
        r %= MAXN;
    }
    void push_front(int v)
    {
        l = (l - 1 + MAXN) % MAXN;
        q[l] = v;
    }
    int front()
    {
        return q[l];
    }
    void pop_front()
    {
        l++;
        l %= MAXN;
    }
    void pop_back()
    {
        r = (r - 1 + MAXN) % MAXN;
    }
};

struct Edge
{
    int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
    N = n;
    tol = 0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int cap,int cost)
{
    edge[tol].to = v;
    edge[tol].cap = cap;
    edge[tol].cost = cost;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].cost = -cost;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}

bool spfa(int s,int t)
{
    DoubleQueue q;
    for(int i = 0; i < N; i++)
    {
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push_back(s);
    while(!q.empty())
    {
        int u = q.front();
        q.pop_front();
        vis[u] = false;
        for(int i = head[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost)
            {
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if(!vis[v])
                {
                    vis[v] = true;
                    if(!q.empty() && dis[v] <= dis[q.front()])
                        q.push_front(v);
                    else q.push_back(v);
                }
            }
        }
    }
    if(pre[t] == -1) return false;
    return true;
}

int Minflow(int s,int t)
{
    int cost = 0;
    while(spfa(s,t))
    {
        int Min = INF;
        for(int i = pre[t]; ~i; i = pre[edge[i ^ 1].to])
        {
            if(Min > edge[i].cap - edge[i].flow)
                Min = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t]; ~i; i = pre[edge[i ^ 1].to])
        {
            edge[i].flow += Min;
            edge[i ^ 1].flow -= Min;
            cost += edge[i].cost * Min;
        }
    }
    return cost;
}

char s[105][105];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    while(~scanf("%d %d",&n,&m) && (n | m))
    {
        init(n * m + 2);
        int ss = n * m,tt = ss + 1;
        for(int i = 0; i < n; i++)
        {
            scanf("%s",s[i]);
            for(int j = 0; j < m; j++)
            {
                if(s[i][j] == 'm')
                    addedge(ss,i * m + j,1,0);
                else if(s[i][j] == 'H') 
                    addedge(i * m + j,tt,1,0);
                if(j + 1 < m)
                {
                    addedge(i * m + j,i * m + j + 1,INF,1);
                    addedge(i * m + j + 1,i * m + j,INF,1);
                }
                if(i + 1 < n)
                {
                    addedge(i * m + j,(i + 1) * m + j,INF,1);
                    addedge((i + 1) * m + j,i * m + j,INF,1);
                }
            }
        }
        printf("%d\n",Minflow(ss,tt));
    }
    return 0;
}

你可能感兴趣的:(费用流,流量)