bzoj 1001

[BeiJing2006]狼抓兔子

题解

平面图的最大流转换为最短路问题, 详见周东的《两极相通》.

示例代码

#include <cstdio>
#include <deque>
#include <bitset>
#include <cstring>
#include <cassert>

using namespace std;
typedef long long int ll;

#define MAXV 2000200
#define MAXE 6000600
struct edge
{
    int v;
    ll w;
    edge *n;
    edge(int vv = 0, ll ww = 0ll, edge *nn = NULL) : v(vv), w(ww), n(nn) { }
}E[MAXE];
int ne;
edge *front[MAXV];
void add_edge(int u, int v, ll w)
{
    E[ne] = edge(v, w, u[front]);
    u[front] = E + ne;
    ne ++;
}

ll d[MAXV];
bitset<MAXV> inq;
ll sp(int s, int t)
{
    deque<int> q;

    memset(d, 0x7f, sizeof d);

    q.push_back(s);
    d[s] = 0;
    inq.set(s);

    while ( !q.empty() )
    {
        int u = q.front();
        q.pop_front(); inq.reset(u);
        for (edge *e = u[front]; e; e = e->n)
        {
            int v = e->v; ll w = e->w;
            if ( d[v] > d[u] + w )
            {
                d[v] = d[u] + w;
                if ( !inq.test(v) )
                {
                    if ( d[v] < d[q.front()] )
                        q.push_front(v);
                    else
                        q.push_back(v);
                    inq.set(v);
                }
            }
        }
    }

    return d[t];
}

#define s (0)
#define t (MAXV-1)

#define lb(x)   (2*(x) - 1)
#define ru(x)   (2*(x))

int main(void)
{
    int N, M;
    ll w;
    scanf("%d%d", &N, &M);
    assert(N > 1 || M > 1);
    /* - */
    /* first line */
    for (int i = 1; i < M; i++)
    {
        scanf("%lld", &w);
        if (1 == N)
        {
            add_edge(s, t, w);
            add_edge(t, s, w);
        } else {
            add_edge(s, ru(i), w);
            add_edge(ru(i), s, w);
        }
    }
    for (int i = 1; i < N - 1; i++)
    {
        int up = 1 + (i - 1) * (M - 1);
        int down = up + M - 1;
        for (int j = 1; j < M; j++)
        {
            scanf("%lld", &w);
            add_edge(lb(up), ru(down), w);
            add_edge(ru(down), lb(up), w);
            up ++; down ++;
        }
    }
    /* last line */
    if (N > 1)
    {
        int block = 1 + (N - 2) * (M - 1);
        for (int i = 1; i < M; i++)
        {
            scanf("%lld", &w);
            add_edge(lb(block), t, w);
            add_edge(t, lb(block), w);
            block ++;
        }
    }
    /* | */
    for (int i = 1; i < N; i++)
    {
        if (1 == M)
        {
            scanf("%lld", &w);
            add_edge(s, t, w);
            add_edge(t, s, w);
        } else {
            int block = 1 + (i - 1) * (M - 1);
            scanf("%lld", &w);
            add_edge(t, lb(block), w); add_edge(lb(block), t, w);
            for (int j = 1; j < M - 1; j++)
            {
                scanf("%lld", &w);
                add_edge(ru(block), lb(block + 1), w);
                add_edge(lb(block + 1), ru(block), w);
                block ++;
            }
            scanf("%lld", &w);
            add_edge(ru(block), s, w);
            add_edge(s, ru(block), w);
        }
    }
    /* \ */
    for (int i = 1; i < N; i++)
    {
        int blk = 1 + (i - 1) * (M - 1);
        for (int j = 1; j < M; j++)
        {
            scanf("%lld", &w);
            add_edge(lb(blk), ru(blk), w);
            add_edge(ru(blk), lb(blk), w);
            blk ++;
        }
    }
    /* --- */
    printf("%lld\n", sp(s, t));
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
Last Updated: 8/18/2018, 2:32:44 PM