只有1行,一个整数,代表最低成本
34
解题思路:费用流。。 没看出来。
建图:对于每个点连一条到汇点的边,容量为u[i],这样可以保障每个月减去u[i]。
起点到每个点也建一条费用为d[i]的边进行补充。然后每个点与它后面的点连一条边,
费用为m。
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
|
#include<iostream>
#include<cstdio>
#define inf 0x7fffffff
#define T 2001
using
namespace
std
;
int
n
,
m
,
s
;
int
cnt
=
1
,
ans
;
int
from
[
2005
]
,
q
[
2005
]
,
dis
[
2005
]
,
head
[
2005
]
;
bool
inq
[
2005
]
;
struct
data
{
int
from
,
to
,
next
,
v
,
c
;
}
e
[
1000001
]
;
void
ins
(
int
u
,
int
v
,
int
w
,
int
c
)
{
cnt
++
;
e
[
cnt
]
.
from
=
u
;
e
[
cnt
]
.
to
=
v
;
e
[
cnt
]
.
v
=
w
;
e
[
cnt
]
.
c
=
c
;
e
[
cnt
]
.
next
=
head
[
u
]
;
head
[
u
]
=
cnt
;
}
void
insert
(
int
u
,
int
v
,
int
w
,
int
c
)
{
ins
(
u
,
v
,
w
,
c
)
;
ins
(
v
,
u
,
0
,
-
c
)
;
}
bool
spfa
(
)
{
for
(
int
i
=
0
;
i
<=
T
;
i
++
)
dis
[
i
]
=
inf
;
int
t
=
0
,
w
=
1
,
i
,
now
;
dis
[
0
]
=
q
[
0
]
=
0
;
inq
[
0
]
=
1
;
while
(
t
!=
w
)
{
now
=
q
[
t
]
;
t
++
;
if
(
t
==
2001
)
t
=
0
;
for
(
int
i
=
head
[
now
]
;
i
;
i
=
e
[
i
]
.
next
)
{
if
(
e
[
i
]
.
v
&&
dis
[
e
[
i
]
.
to
]
>
dis
[
now
]
+
e
[
i
]
.
c
)
{
from
[
e
[
i
]
.
to
]
=
i
;
dis
[
e
[
i
]
.
to
]
=
dis
[
now
]
+
e
[
i
]
.
c
;
if
(
!
inq
[
e
[
i
]
.
to
]
)
{
inq
[
e
[
i
]
.
to
]
=
1
;
q
[
w
++
]
=
e
[
i
]
.
to
;
if
(
w
==
2001
)
w
=
0
;
}
}
}
inq
[
now
]
=
0
;
}
if
(
dis
[
T
]
==
inf
)
return
0
;
return
1
;
}
void
mcf
(
)
{
int
i
,
x
=
inf
;
i
=
from
[
T
]
;
while
(
i
)
{
x
=
min
(
e
[
i
]
.
v
,
x
)
;
i
=
from
[
e
[
i
]
.
from
]
;
}
i
=
from
[
T
]
;
while
(
i
)
{
e
[
i
]
.
v
-=
x
;
e
[
i
^
1
]
.
v
+=
x
;
ans
+=
x
*
e
[
i
]
.
c
;
i
=
from
[
e
[
i
]
.
from
]
;
}
}
int
main
(
)
{
scanf
(
"%d%d%d"
,
&n
,
&m
,
&s
)
;
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
{
int
x
;
scanf
(
"%d"
,
&x
)
;
insert
(
i
,
T
,
x
,
0
)
;
}
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
{
int
x
;
scanf
(
"%d"
,
&x
)
;
insert
(
0
,
i
,
inf
,
x
)
;
}
for
(
int
i
=
1
;
i
<
n
;
i
++
)
insert
(
i
,
i
+
1
,
s
,
m
)
;
while
(
spfa
(
)
)
mcf
(
)
;
printf
(
"%d"
,
ans
)
;
return
0
;
}
|