TIOJ1236 工廠生產問題

題目連結:https://tioj.ck.tp.edu.tw/problems/1236
題意:工廠有很多機器,有些機器會利用原料(或是半成品)產出半成品,接著當天給其他機器繼續產出,最終會有機器產出成品,每台機器有各自的產能上限(以天為單位),問一天最高的產能為多少。

解法:這題是需要拆點的最大流,把每個機器拆成兩點 $x_{in}$ 和 $x_{out}$,拿源點連接原料生產的機器,拿產出最後成品的機器連結匯點,對於有上下流關係的兩個機器,將上流的 $out$ 點,連到下流的 $in$ 點,然後跑最大流就能得出答案。這是我又用成無向圖吃了 WA,我對圖是有向圖或是無向圖的判斷需要精準一點。

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
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int,int>;
using PLL = pair<LL, LL>;
const LL INF = 1e18;
const int MXN = 0;
const int MXV = 205;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FOR(i, L, R) for(int i = L; i != (int)R; ++i)
#define FORD(i, L, R) for(int i = L; i != (int)R; --i)
#define IOS cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);

template <typename T>
struct Dinic
{
int n, s, t, level[MXV], now[MXV];
struct Edge
{
int v;
T rf; // rf: residual flow
int re;
};
vector<Edge> e[MXV];
void init(int _n, int _s, int _t)
{
n = _n;
s = _s;
t = _t;
for (int i = 0; i <= n; i++)
{
e[i].clear();
}
}
void add_edge(int u, int v, T f)
{
// cout << u << ' ' << v << ' ' << f << '\n';
e[u].push_back({v, f, (int)e[v].size()});
// e[v].push_back({u, f, (int)e[u].size() - 1});
// for directional graph
e[v].push_back({u, 0, (int)e[u].size() - 1});
}
bool bfs()
{
fill(level, level + n + 1, -1);
queue<int> q;
q.push(s);
level[s] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto it : e[u])
{
if (it.rf > 0 && level[it.v] == -1)
{
level[it.v] = level[u] + 1;
q.push(it.v);
}
}
}
return level[t] != -1;
}
T dfs(int u, T limit)
{
if (u == t)
return limit;
T res = 0;
while (now[u] < (int)e[u].size())
{
Edge &it = e[u][now[u]];
if (it.rf > 0 && level[it.v] == level[u] + 1)
{
T f = dfs(it.v, min(limit, it.rf));
res += f;
limit -= f;
it.rf -= f;
e[it.v][it.re].rf += f;
if (limit == 0)
{
return res;
}
}
else
{
++now[u];
}
}
if (!res)
{
level[u] = -1;
}
return res;
}
T flow(T res = 0)
{
while (bfs())
{
T tmp;
memset(now, 0, sizeof(now));
do{
tmp = dfs(s, INF);
res += tmp;
}while(tmp);
}
return res;
}
};

int main()
{
IOS;
int n, m;
Dinic<LL> dinic;
bitset<MXV> pin, pout;
pin.set();
pout.set();
cin >> n;
int s = 0, t = 2 * n + 1;
dinic.init(t, s, t);
FOR(i, 1, n + 1)
{
LL x;
cin >> x;
dinic.add_edge(i, i + n, x);
}
cin >> m;
FOR(i, 0, m)
{
int x, y;
cin >> x >> y;
pin[y] = false;
pout[x] = false;
dinic.add_edge(x + n, y, INF);
}
FOR(i, 1, n + 1)
{
if(pin[i])
{
dinic.add_edge(s, i, INF);
}
if(pout[i])
{
dinic.add_edge(i + n, t, INF);
}
}
cout << dinic.flow() << '\n';
}

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts