-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13161_분단의_슬픔.cpp
109 lines (91 loc) · 2.07 KB
/
13161_분단의_슬픔.cpp
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
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;
int N;
int flow[502][502];
int level[502];
int cnt[502];
bool bfs()
{
fill(level, level + (N + 2), -1);
level[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty())
{
int here = q.front();
q.pop();
for (int there = 0; there < N + 2; there++)
{
if (level[there] != -1)
continue;
if (flow[here][there] <= 0)
continue;
level[there] = level[here] + 1;
q.push(there);
}
}
return level[N + 1] != -1;
}
int dfs(int here, int f)
{
if (here == N + 1)
return f;
for (int &there = cnt[here]; there <= N + 1; there++)
{
if (level[here] >= level[there])
continue;
if (flow[here][there] <= 0)
continue;
int nflow = dfs(there, min(f, flow[here][there]));
if (nflow)
{
flow[here][there] -= nflow;
flow[there][here] += nflow;
return nflow;
}
}
return 0;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++)
{
int team;
cin >> team;
if (team == 1)
flow[0][i] = INF;
if (team == 2)
flow[i][N + 1] = INF;
}
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
int num;
cin >> num;
flow[i][j] = flow[j][i] = num;
}
}
int answer = 0;
while (bfs())
{
fill(cnt, cnt + (N + 2), 0);
int res = 0;
while (res = dfs(0, INF))
answer += res;
}
cout << answer << '\n';
for (int i = 1; i <= N; i++)
if (level[i] != -1)
cout << i << ' ';
cout << '\n';
for (int i = 1; i <= N; i++)
if (level[i] == -1)
cout << i << ' ';
cout << '\n';
return 0;
}