Cod sursa(job #1726686)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 8 iulie 2016 18:22:53
Problema Cc Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cc.in");
ofstream fout("cc.out");

const int NMAX = 103;
const int INF = 0x3f3f3f3f;

int n; int s; int d;

int cost[NMAX][NMAX];
int rez[NMAX][NMAX];
int ant[NMAX];

int c;

void read() {

	fin >> n ;
	s = 0;
	d = 2 * n + 1;

	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j) {
			rez[i][j + n] = 1;
			fin >> cost[i][j + n];
			cost[j + n][i] = -cost[i][j + n];
		}
}

bool bf(int s, int d) {

	int dist[NMAX];
	int inf = 0;
	int sup = 2 * n + 1;

	for(int i = inf; i <= sup ; ++i)
		dist[i] = INF;

	dist[s] = 0;

	queue<int> q;
	q.push(s);

	while(q.empty() == false) {
		int node = q.front();
		q.pop();

		for(int i = inf; i <= sup; ++i)
			if(dist[i] > dist[node] + cost[node][i] && rez[node][i] > 0) {
				q.push(i);
				dist[i] = dist[node] + cost[node][i];
				ant[i] = node;
			}
	}

	return dist[d] == INF ? false : true;
}

void solve() {

	for(int i = 1; i <= n; ++i)
		rez[s][i] = 1, rez[i + n][d] = 1;

	while( bf(s, d) ) {
		for(int x = d ; x != s; x = ant[x]) {

			rez[ant[x]][x] -= 1;
			rez[x][ant[x]] += 1;
			c += cost[ant[x]][x];
			//cout << cost[ant[x]][x] << '\n'; 
		}
	}

	fout << c << '\n'; 
}

int main() {

	read();
	solve();
	return 0;
}