Pagini recente » Cod sursa (job #731332) | Cod sursa (job #1514624) | Cod sursa (job #835722) | Cod sursa (job #1801742) | Cod sursa (job #1728434)
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("algola.in");
ofstream fout("algola.out");
typedef pair<int, int> pii;
const int dim = 5005;
const int source = 0;
const int dest = 5001;
const int inf = 70;
int n, m;
vector<pii> inputG[dim];
vector<int> g[dim];
int C[dim][dim], F[dim][dim];
bool vis[dim];
int parent[dim];
inline void addEdge(int from, int to, int capacity) {
g[from].push_back(to);
g[to].push_back(from);
C[from][to] = capacity;
}
bool bfs(void) {
memset(vis, false, sizeof vis);
queue<int> que;
que.push(source);
vis[source] = true;
while (!que.empty()) {
int node = que.front();
que.pop();
if (node == dest)
continue;
for (auto& it : g[node]) {
if (vis[it] || F[node][it] == C[node][it])
continue;
vis[it] = true;
parent[it] = node;
que.push(it);
}
}
return vis[dest];
}
int getAddFlow(void) {
int maxFlow = 0;
while (bfs()) {
for (auto& it : g[dest]) {
if (!vis[it] || F[it][dest] == C[it][dest])
continue;
parent[dest] = it;
int addFlow = inf;
for (int i = dest; i != source; i = parent[i])
addFlow = min(addFlow, C[parent[i]][i] - F[parent[i]][i]);
maxFlow += addFlow;
for (int i = dest; i != source; i = parent[i]) {
F[parent[i]][i] += addFlow;
F[i][parent[i]] += addFlow;
}
}
}
return maxFlow;
}
int main() {
fin >> n >> m;
int total = 0;
for (int i = 1; i <= n; ++i) {
int x; fin >> x;
total += x;
addEdge(source, i, x);
}
for (int i = 1; i <= m; ++i) {
int x, y, capacity;
fin >> x >> y >> capacity;
inputG[x].push_back(pii(y, capacity));
inputG[y].push_back(pii(x, capacity));
}
addEdge(1, dest, inf);
int ans = 0, maxFlow = getAddFlow();
while (maxFlow < total) {
++ans;
for (int i = 1; i <= n; ++i) {
addEdge(n * (ans - 1) + i, n * ans + i, inf);
for (auto& it : inputG[i]) {
addEdge(n * (ans - 1) + i, n * ans + it.first, it.second);
}
}
addEdge(ans * n + 1, dest, inf);
maxFlow += getAddFlow();
}
fout << ans << '\n';
return 0;
}
//Trust me, I'm the Doctor!