Pagini recente » Cod sursa (job #1557348) | Cod sursa (job #573860) | Cod sursa (job #1564567) | Cod sursa (job #2025268) | Cod sursa (job #2059611)
#include <bits/stdc++.h>
using namespace std;
vector<map<int, int>> G;
vector<bool> Vis;
int DFS(int node, int dest, int flow) {
if (node == dest) return flow;
if (Vis[node] or flow == 0) return 0;
Vis[node] = true;
for (auto p : G[node]) {
int vec, cap;
tie(vec, cap) = p;
int now = DFS(vec, dest, min(flow, cap));
if (now > 0) {
G[node][vec] -= now;
G[vec][node] += now;
return now;
}
}
return 0;
}
int main() {
ifstream cin("algola.in");
ofstream cout("algola.out");
int n, m; cin >> n >> m;
vector<tuple<int, int, int>> edges(m);
int tot = 0;
G.resize(n + 2);
for (int i = 0; i < n; ++i) {
int pop; cin >> pop;
tot += pop;
G[n][i] = pop;
}
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a; --b;
edges[i] = make_tuple(a, b, c);
}
G[0][n + 1] = 1e9;
int flow = 0, dayz = 0;
while (flow < tot) {
Vis.assign(n + 2, false);
int incr = DFS(n, n + 1, 1e9);
flow += incr;
if (incr == 0) {
++dayz;
for (auto p : edges) {
int a, b, c; tie(a, b, c) = p;
G[a][b] += c;
G[b][a] += c;
}
}
}
cout << dayz << endl;
return 0;
}