Pagini recente » Cod sursa (job #3272648) | Cod sursa (job #2968582) | Cod sursa (job #2648881) | Cod sursa (job #2648952) | Cod sursa (job #1513893)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define MAXN 350
#define INFINIT 2000000000
using namespace std;
typedef queue <int> QQ;
QQ Q;
typedef vector <int> lst;
int n, m, s, d, drum = 0;
lst G[MAXN + 1];
int cap[MAXN + 1][MAXN + 1], cost[MAXN + 1][MAXN + 1];
int dist[MAXN + 1], pre[MAXN + 1];
bool inQ[MAXN + 1];
long long BF() {
for (int i = 1 ; i <= n ; ++i) {
dist[i] = INFINIT;
pre[i] = -1;
inQ[i] = 0;
}
dist[s] = 0;
while (!Q.empty())
Q.pop();
Q.push(s);
inQ[s] = 1;
while (!Q.empty()) {
int node = Q.front();
Q.pop();
inQ[node] = false;
for (lst :: iterator it = G[node].begin() ; it != G[node].end() ; ++it) {
if (cap[node][*it] > 0 && dist[node] + cost[node][*it] < dist[*it]) {
dist[*it] = dist[node] + cost[node][*it];
pre[*it] = node;
if (!inQ[*it]) {
Q.push(*it);
inQ[*it] = true;
}
}
}
}
if (dist[d] != INFINIT) {
drum = true;
int minv = INFINIT;
for (int i = d ; i != s ; i = pre[i])
if (cap[pre[i]][i] < minv)
minv = cap[pre[i]][i];
for (int i = d ; i != s ; i = pre[i]) {
cap[pre[i]][i] -= minv;
cap[i][pre[i]] += minv;
}
return minv * dist[d];
}
return 0;
}
int main () {
ifstream cin("cc.in");
ofstream cout("cc.out");
cin >> n;
for (int i = 1 ; i <= n ; ++i)
for (int j = 1 ; j <= n ; ++j) {
cin >> cap[i][j];
long long sol = 0;
drum = 1;
while (drum) {
drum = 0;
sol += BF();
}
cout << sol << "\n";
return 0;
}