Pagini recente » Cod sursa (job #1544115) | Cod sursa (job #2782053) | Cod sursa (job #2259087) | Cod sursa (job #2473321) | Cod sursa (job #1513898)
#include <iostream>
#include <fstream>
#include <queue>
#define MAXN 100
#define MAXN2 200
#define INFINIT 99999999
using namespace std;
typedef queue <int> QQ;
QQ Q;
int n, drum;
int cap[MAXN2 + 2][MAXN2 + 2], cost[MAXN2 + 2][MAXN2 + 2];
int pre[MAXN2 + 2], dist[MAXN2 + 2];
bool inQ[MAXN2 + 2];
int BF() {
while (!Q.empty())
Q.pop();
for (int i = 1 ; i < (2 * n) + 2 ; ++i) {
dist[i] = INFINIT;
inQ[i] = false;
pre[i] = -1;
}
dist[0] = 0;
Q.push(0);
inQ[0] = true;
while (!Q.empty()) {
int node = Q.front();
Q.pop();
inQ[node] = false;
for (int i = 1 ; i < (2 * n) + 2 ; ++i) {
if (cap[node][i] && dist[node] + cost[node][i] < dist[i]) {
dist[i] = dist[node] + cost[node][i];
pre[i] = node;
if (!inQ[i]) {
Q.push(i);
inQ[i] = true;
}
}
}
}
int d = (2 * n) + 1;
if (dist[d] != INFINIT) {
drum = true;
int minv = INFINIT;
int node = d;
while (node != 0) {
if (cap[pre[node]][node] < minv)
minv = cap[pre[node]][node];
node = pre[node];
}
node = d;
while (node != 0) {
cap[pre[node]][node] -= minv;
cap[node][pre[node]] += minv;
node = pre[node];
}
return 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 >> cost[i][j + n];
cost[j + n][i] = -cost[i][j + n];
cap[i][j + n] = INFINIT;
}
}
for (int i = 1 ; i <= n ; ++i) {
cap[0][i] = 1;
cap[i + n][(2 * n) + 1] = 1;
}
drum = 1;
int sol = 0;
while (drum) {
drum = 0;
sol += BF();
}
cout << sol << "\n";
return 0;
}