Pagini recente » Cod sursa (job #612157) | Cod sursa (job #1307499) | Cod sursa (job #2383842) | Cod sursa (job #2400280) | Cod sursa (job #2420984)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
#define inf (1 << 20)
const int kNmax = 1005;
class Task {
public:
void solve() {
read_input();
print_output(get_result());
}
private:
int n;
int m;
vector<int> adj[kNmax];
int C[kNmax][kNmax];
int used[kNmax];
int p[kNmax];
int maxflow = 0;
void read_input() {
ifstream fin("in");
fin >> n >> m;
memset(C, 0, sizeof C);
for (int i = 1, x, y, z; i <= m; i++) {
fin >> x >> y >> z;
adj[x].push_back(y);
adj[y].push_back(x);
C[x][y] += z;
}
fin.close();
}
bool bfs() {
int node;
memset(used, 0, sizeof(used));
queue<int> q;
q.push(1);
used[1] = 1;
while (!q.empty()) {
node = q.front();
q.pop();
if (node == n) {
continue;
}
for (auto it : adj[node]) {
if (used[it] || !C[node][it]) {
continue;
}
p[it] = node;
used[it] = 1;
q.push(it);
}
}
return used[n];
}
int get_result() {
/*
TODO: Calculati fluxul maxim pe graful orientat dat.
Sursa este nodul 1.
Destinatia este nodul n.
In adj este stocat graful neorientat obtinut dupa ce se elimina orientarea
arcelor, iar in C sunt stocate capacitatile arcelor.
De exemplu, un arc (x, y) de capacitate z va fi tinut astfel:
C[x][y] = z, adj[x] contine y, adj[y] contine x.
*/
int node;
while (bfs()) {
for (auto it : adj[n]) {
if (!used[it] || !C[it][n]) {
continue;
}
p[n] = it;
int flow = inf;
for (node = n; node != 1; node = p[node]) {
flow = min(flow, C[p[node]][node]);
}
for (node = n; node != 1; node = p[node]) {
C[p[node]][node] -= flow;
C[node][p[node]] += flow;
}
maxflow += flow;
}
}
return maxflow;
}
void print_output(int result) {
ofstream fout("out");
fout << result << '\n';
fout.close();
}
};
int main() {
Task *task = new Task();
task->solve();
delete task;
return 0;
}