Pagini recente » Clasament simulareoji2015p2 | Cod sursa (job #2669059) | Cod sursa (job #1092122) | Cod sursa (job #2701569) | Cod sursa (job #1214732)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1024;
int graph[NMAX][NMAX];
int resid[NMAX][NMAX];
int shptr[NMAX];
bool vistd[NMAX];
int N, M;
int pfs()
{
// 1 -> N;
priority_queue <pair <int, pair <int, int> > > pq;
pq.push(make_pair(INT_MAX, make_pair(0, 1)));
for (int i = 0; i < NMAX; ++i) {
vistd[i] = false;
shptr[i] = 0;
}
while (!pq.empty()) {
int magp = pq.top().first;
int from = pq.top().second.first;
int node = pq.top().second.second;
pq.pop();
if (vistd[node]) {
continue;
}
// cout << node << " ";
vistd[node] = true;
shptr[node] = from;
if (node == N) {
// cout << "\n";
return magp;
}
vistd[node] = true;
for (int i = 1; i <= N; ++i) {
if (resid[node][i] > 0) {
if (true) {
pq.push(make_pair(min(magp, resid[node][i]), make_pair(node, i)));
}
}
}
}
// cout << "\n";
return 0;
}
int main()
{
ifstream in("maxflow.in");
ofstream out("maxflow.out");
in >> N >> M;
for (int a, b, c, i = 0; i < M; ++i) {
in >> a >> b >> c;
graph[a][b] = c;
}
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (graph[i][j] > 0) {
resid[i][j] = graph[i][j];
} else if (graph[j][i] > 0) {
resid[i][j] = 0;
} else {
resid[i][j] = -1;
}
}
}
int a = 0;
int flow = 0;
while ((a = pfs()) > 0) {
flow += a;
int x = N;
while (x != 1) {
// cout << "\t" << shptr[x] << "\t->\t" << x;
// cout << "\t|\t" << resid[shptr[x]][x] << "\t" << resid[x][shptr[x]] << "\t" << a << "\n";
// cout << "\t\t\t";
resid[shptr[x]][x] -= a;
resid[x][shptr[x]] += a;
// cout << "\t|\t" << resid[shptr[x]][x] << "\t" << resid[x][shptr[x]] << "\t" << a << "\n";
x = shptr[x];
}
// cout << "\n";
}
// cout << flow << "\n";
out << flow << "\n";
return 0;
}