Cod sursa(job #2596769)

Utilizator alex2209alexPavel Alexandru alex2209alex Data 10 aprilie 2020 13:08:17
Problema Cc Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("cc.in");
ofstream g("cc.out");
//--------------------------------------------------------------------
///Globale
const int INF = 1000000000;
const short MAX = 202;
short n,cost[MAX][MAX],cap[MAX][MAX],sursa,destinatie,flux[MAX][MAX],par[MAX];
vector<short>muchii[MAX];
int d[MAX],raspuns;
bitset<MAX>ap;
//--------------------------------------------------------------------
///Functii
void citire();
void rezolvare();
void afisare();
//--------------------------------------------------------------------
int main()
{
	citire();
	rezolvare();
	afisare();
	return 0;
}
//--------------------------------------------------------------------
void afisare()
{
    g << raspuns;
}
//--------------------------------------------------------------------
bool Bellman_Ford()
{
	for(int i = 0; i <= destinatie; ++i)
		d[i] = INF;
	d[sursa] = 0;
	queue<short>q;
	q.push(sursa);
	ap[sursa] = 1;
	while(!q.empty())
	{
		int nod = q.front();
		q.pop();
		ap[nod] = 0;
		for(auto it : muchii[nod])
			if(d[nod] + cost[nod][it] < d[it] && flux[nod][it] < cap[nod][it])
			{
			    d[it] = d[nod] + cost[nod][it];
			    par[it] = nod;
			    if(!ap[it])
                {
                    q.push(it);
                    ap[it] = 1;
                }
			}
	}
	if(d[destinatie] != INF)
        return 1;
    return 0;
}
//--------------------------------------------------------------------
void rezolvare()
{
	sursa = 0;
	destinatie = 2 * n + 1;
	for(int i = 1; i <= n; ++i)
	{
		cap[sursa][i] = 1;
		cap[i + n][destinatie] = 1;
		muchii[sursa].push_back(i);
		muchii[i + n].push_back(destinatie);
	}
	while(Bellman_Ford())
	{
	    int nod = destinatie;
	    while(nod != sursa)
        {
            flux[par[nod]][nod]++;
            flux[nod][par[nod]]--;
            nod = par[nod];
        }
        raspuns += d[destinatie];
	}
}
//--------------------------------------------------------------------
void citire()
{
	f >> n;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
		{
			f >> cost[i][j + n];
			cost[j + n][i] = -cost[i][j + n];
			muchii[i].push_back(j + n);
			muchii[j + n].push_back(i);
			cap[i][j + n] = 1;
		}
}