Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4013 数字梯形问题

P4013 数字梯形问题

权值在点上,所以一定要拆点的。

子任务 $1$ :路径不相交。即每个点只能走一次,即点中的流量为 $1$ ,边上的流量无所谓,因为出发点最多只有 $1$ 流量。

子任务 $2$ :仅在点上交。即每条边只能走一次,即点中的流量为 $inf$ ,边上的流量为 $1$ 。

子任务 $3$ :允许相交,流量为 $inf$ 。

查看代码
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
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 2e3 + 10, M = 4e3 + 10;
bool vis[N];
int tot = -1, cost[40][40], id[40][40];
int n, m, st, ed, d[N], pre[N], incf[N];
int idx, hd[N], nxt[M], edg[M], wt[M], f[M];
bool spfa()
{
memset(d, -0x3f, sizeof d);
memset(incf, 0, sizeof incf);
queue <int> q;
q.push(st);
vis[st] = true;
d[st] = 0;
incf[st] = INF;
while (!q.empty())
{
int t = q.front();
q.pop();
vis[t] = false;
for (int i = hd[t]; ~i; i = nxt[i])
if (d[t] + f[i] > d[edg[i]] && wt[i])
{
d[edg[i]] = d[t] + f[i];
pre[edg[i]] = i;
incf[edg[i]] = min(wt[i], incf[t]);
if (!vis[edg[i]])
{
q.push(edg[i]);
vis[edg[i]] = true;
}
}
}
return incf[ed] > 0;
}
int ek()
{
int res = 0;
while (spfa())
{
int t = incf[ed];
res += t * d[ed];
for (int i = ed; i != st; i = edg[pre[i] ^ 1])
{
wt[pre[i]] -= t;
wt[pre[i] ^ 1] += t;
}
}
return res;
}
void add(int a, int b, int c, int d)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
f[idx] = d;
}
int main()
{
cin >> m >> n;
st = ++tot;
ed = ++tot;
for (int i = 1; i <= n; i++)
for (int j = 1; j < m + i; j++)
{
cin >> cost[i][j];
id[i][j] = ++tot;
}
memset(hd, -1, sizeof hd);
idx = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j < m + i; j++)
{
int t = id[i][j];
add(t << 1, t << 1 | 1, 1, cost[i][j]);
add(t << 1 | 1, t << 1, 0, -cost[i][j]);
if (i == 1)
{
add(st, t << 1, 1, 0);
add(t << 1, st, 0, 0);
}
if (i == n)
{
add(t << 1 | 1, ed, 1, 0);
add(ed, t << 1 | 1, 0, 0);
}
else
{
int h1 = id[i + 1][j],
h2 = id[i + 1][j + 1];
add(t << 1 | 1, h1 << 1, 1, 0);
add(h1 << 1, t << 1 | 1, 0, 0);
add(t << 1 | 1, h2 << 1, 1, 0);
add(h2 << 1, t << 1 | 1, 0, 0);
}
}
cout << ek() << endl;
memset(hd, -1, sizeof hd);
idx = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j < m + i; j++)
{
int t = id[i][j];
add(t << 1, t << 1 | 1, INF, cost[i][j]);
add(t << 1 | 1, t << 1, 0, -cost[i][j]);
if (i == 1)
{
add(st, t << 1, 1, 0);
add(t << 1, st, 0, 0);
}
if (i == n)
{
add(t << 1 | 1, ed, INF, 0);
add(ed, t << 1 | 1, 0, 0);
}
else
{
int h1 = id[i + 1][j],
h2 = id[i + 1][j + 1];
add(t << 1 | 1, h1 << 1, 1, 0);
add(h1 << 1, t << 1 | 1, 0, 0);
add(t << 1 | 1, h2 << 1, 1, 0);
add(h2 << 1, t << 1 | 1, 0, 0);
}
}
cout << ek() << endl;
memset(hd, -1, sizeof hd);
idx = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j < m + i; j++)
{
int t = id[i][j];
add(t << 1, t << 1 | 1, INF, cost[i][j]);
add(t << 1 | 1, t << 1, 0, -cost[i][j]);
if (i == 1)
{
add(st, t << 1, 1, 0);
add(t << 1, st, 0, 0);
}
if (i == n)
{
add(t << 1 | 1, ed, INF, 0);
add(ed, t << 1 | 1, 0, 0);
}
else
{
int h1 = id[i + 1][j],
h2 = id[i + 1][j + 1];
add(t << 1 | 1, h1 << 1, INF, 0);
add(h1 << 1, t << 1 | 1, 0, 0);
add(t << 1 | 1, h2 << 1, INF, 0);
add(h2 << 1, t << 1 | 1, 0, 0);
}
}
cout << ek();
return 0;
}