Pagini recente » Cod sursa (job #3209473) | Cod sursa (job #1139630)
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <queue>
#define son first
#define cost second
using namespace std;
typedef pair <short, int> graph_node;
const int NMAX = 203, INFI = 2e9;
vector <graph_node> BG[NMAX];
bitset <NMAX> flow[NMAX];
queue <short> Q;
pair <short, int> adj[NMAX][NMAX];
int N, dist[NMAX], T[NMAX], ans;
inline bool bellman_ford () {
vector <graph_node> :: iterator i;
short node;
for (node = 1; node <= 2 * N + 1; ++node)
dist[node] = INFI;
for (Q.push (0); !Q.empty (); Q.pop ()) {
node = Q.front ();
for (i = BG[node].begin (); i != BG[node].end (); ++i)
if (flow[node][i->son] && dist[i->son] > dist[node] + i->cost) {
dist[i->son] = dist[node] + i->cost;
Q.push (i->son);
T[i->son] = node;
}
}
if (dist[2 * N + 1] == INFI)
return 0;
ans += dist[2 * N + 1];
for (node = 2 * N + 1; node; node = T[node]) {
flow[T[node]][node] = 0;
flow[node][T[node]] = 1;
}
return 1;
}
int main () {
freopen ("cc.in", "r", stdin);
freopen ("cc.out", "w", stdout);
int i, j, a;
scanf ("%d", &N);
for (i = 1; i <= N; ++i)
for (j = 1; j <= N; ++j) {
scanf ("%d", &a);
BG[i].push_back (make_pair (N + j, a));
BG[N + j].push_back (make_pair (i, -a));
flow[i][N + j] = 1;
}
for (i = 1; i <= N; ++i) {
BG[0].push_back (make_pair (i, 0));
flow[0][i] = 1;
BG[N + i].push_back (make_pair (2 * N + 1, 0));
flow[N + i][2 * N + 1] = 1;
}
while (bellman_ford ());
printf ("%d", ans);
}