Pagini recente » Cod sursa (job #2687114) | Cod sursa (job #2035737) | Cod sursa (job #2279126) | Cod sursa (job #1269162) | Cod sursa (job #2596769)
#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;
}
}