hdoj Battle 解法最大权闭包
1
#include
"
stdio.h
"
2
#include
"
string.h
"
3
#include
<
queue
>
4
using
namespace
std;
5
#define
N 1000
6
#define
M 1000000
7
#define
INF 1000000000
8
9
int
f[N],h[N],hv[N];
10
int
c[N][N];
11
int
n,m,src,des;
12
int
augc;
13
int
flow;
14
int
sum;
15
16
void
Insert(
int
fr,
int
to,
int
d)
17
{
18
c[fr][to]
=
d;
19
}
20
21
bool
aug(
int
v)
22
{
23
int
i,minh
=
n
-
1
;
24
int
augco
=
augc;
25
if
(v
==
des)
26
{
27
flow
+=
augc;
28
return
true
;
29
}
30
for
(i
=
0
;i
<
n;i
++
)
31
{
32
if
(c[v][i]
>
0
)
33
{
34
if
(h[v]
==
h[i]
+
1
)
35
{
36
if
(augc
>
c[v][i])
37
augc
=
c[v][i];
38
if
(aug(i))
break
;
39
if
(h[src]
>=
n)
40
{
41
return
false
;
42
}
43
augc
=
augco;
44
}
45
if
(minh
>
h[i])
46
minh
=
h[i];
47
}
48
}
49
if
(i
==
n)
50
{
51
hv[h[v]]
--
;
52
if
(hv[h[v]]
==
0
) h[src]
=
n;
53
h[v]
=
minh
+
1
;
54
hv[h[v]]
++
;
55
return
false
;
56
}
57
else
58
{
59
c[v][i]
-=
augc;
60
c[i][v]
+=
augc;
61
return
true
;
62
}
63
}
64
void
SAP()
65
{
66
memset(h,
0
,
sizeof
(h));
67
memset(hv,
0
,
sizeof
(hv));
68
hv[
0
]
=
n;
69
flow
=
0
;
70
while
(h[src]
<
n)
71
{
72
augc
=
INF;
73
aug(src);
74
}
75
printf(
"
%d\n
"
,sum
-
flow);
76
}
77
int
main()
78
{
79
int
i,m,a,b;
80
while
(scanf(
"
%d %d
"
,
&
n,
&
m)
!=
EOF)
81
{
82
memset(c,
0
,
sizeof
(c));
83
src
=
0
;
84
des
=
n
+
1
;
85
sum
=
0
;
86
for
(i
=
1
;i
<=
n;i
++
)
87
{
88
scanf(
"
%d
"
,
&
f[i]);
89
if
(f[i]
>
0
)
90
{
91
Insert(src,i,f[i]);
92
sum
+=
f[i];
93
}
94
else
95
{
96
Insert(i,des,
-
f[i]);
97
}
98
}
99
for
(i
=
0
;i
<
m;i
++
)
100
{
101
scanf(
"
%d %d
"
,
&
a,
&
b);
102
Insert(a,b,INF);
103
}
104
n
+=
2
;
105
SAP();
106
}
107
return
0
;
108
}
109
|
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 30 | 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 | 1 | 2 | 3 | 4 |
|
导航
统计
常用链接
留言簿
随笔分类(1)
随笔档案(2)
文章分类(15)
文章档案(7)
搜索
最新评论
阅读排行榜
评论排行榜
|
|