BZOJ 1001 狼抓兔子 【网络流】

Description

现在小朋友们最喜欢的”喜羊羊与灰太狼”,话说灰太狼抓羊不到,但抓兔子还是比较在行的,
而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:

左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路
1:(x,y)<==>(x+1,y)
2:(x,y)<==>(x,y+1)
3:(x,y)<==>(x+1,y+1)
道路上的权值表示这条路上最多能够通过的兔子数,道路是无向的. 左上角和右下角为兔子的两个窝,
开始时所有的兔子都聚集在左上角(1,1)的窝里,现在它们要跑到右下解(N,M)的窝中去,狼王开始伏击
这些兔子.当然为了保险起见,如果一条道路上最多通过的兔子数为K,狼王需要安排同样数量的K只狼,
才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的
狼的数量要最小。因为狼还要去找喜羊羊麻烦.

Input

第一行为N,M.表示网格的大小,N,M均小于等于1000.
接下来分三部分
第一部分共N行,每行M-1个数,表示横向道路的权值.
第二部分共N-1行,每行M个数,表示纵向道路的权值.
第三部分共N-1行,每行M-1个数,表示斜向道路的权值.
输入文件保证不超过10M

Output

输出一个整数,表示参与伏击的狼的最小数量.

Sample Input

3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6

Sample Output

14

HINT

 2015.4.16新加数据一组,可能会卡掉从前可以过的程序。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf = 1e9;
int s, t;
int fst[1000005], nxt[6000005], tot = 1;
int d[1000005];
int cur[1000005];
struct qer
{
    int to, cap;
}es[6000005];
queue <int> q;
void read(int &a)
{
    a = 0;
    char c = getchar();
    while (c < '0' || c > '9')
        c = getchar();
    while(c <= '9' && c >= '0')
    {
        a *= 10;
        a += c - '0';
        c = getchar();
    }
}
void build(int f, int t, int dd)
{
    es[++tot] = (qer){t, dd};
    nxt[tot] = fst[f];
    fst[f] = tot;
}
void addedge(int f, int t, int dd)
{
    build (f, t, dd);
    build (t, f, dd);
}
bool bfs()
{
    for (int i = 1; i <= t; i++)
        d[i] = 0;
    d[s] = 1;
    q.push(s);
    int u, v;
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        for (int i = fst[u]; i; i = nxt[i])
        {
            v = es[i].to;
            if (es[i].cap > 0 && !d[v])
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    if (d[t])
        return 1;
    return 0;
}
int dfs(int u, int a)
{
    if (u == t || a == 0)
        return a;
    int &i = cur[u], ans = 0;
    int v;
    for ( ; i; i = nxt[i])
    {
        v = es[i].to;
        if (es[i].cap > 0 && d[v] == d[u] + 1)
        {
            int f = dfs(v, min(a, es[i].cap));
            ans += f;
            a -= f;
            es[i].cap -= f;
            es[i^1].cap += f;
        }
        if (a == 0)
            break;
    }
    return ans;
}
int maxflow()
{
    int ans = 0;
    while (bfs())
    {
        for (int i = 1; i <= t; i++)
            cur[i] = fst[i];
        ans += dfs(s, inf);
    }
    return ans;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    s = 1;
    t = n*m;
    int x, w;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            read(w);
            x = (i-1)*m+j;
            addedge(x, x+1, w);
        }
    }
    int tot = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            read(w);
            tot++;
            addedge(tot, tot + m, w);
        }
    }
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j < m; j++)
        {
            read(w);
            x = (i-1)*m+j;
            addedge(x, x+m+1, w);
        }
    }
    printf("%d", maxflow());
    return 0;
}

 

上一篇
下一篇