Pagini recente » Cod sursa (job #2327746) | Cod sursa (job #2894079) | Cod sursa (job #1102457) | Cod sursa (job #1315820) | Cod sursa (job #906141)
Cod sursa(job #906141)
#include <fstream>
#include <queue>
#define N 500
#define oo int(2e9)
using namespace std;
queue <int> Q;
bool inq[N];
int sol, i, j, n, x, S, D;
int prev[N], Cost[N][N], Dist[N], C[N][N];
bool Bellman()
{
for(i = 0; i <= 2*n + 1; i++) Dist[i] = oo;
Dist[S] = 0;
Q.push(S);
while(!Q.empty())
{
x = Q.front();
Q.pop();
inq[x] = 0;
for(i = 0; i <= D; i++)
if(C[x][i] and Dist[i] > Dist[x] + Cost[x][i])
{
Dist[i] = Dist[x] + Cost[x][i];
prev[i] = x;
if(!inq[i])
{
inq[i] = 1;
Q.push(i);
}
}
}
return Dist[D] != oo;
}
int main()
{
ifstream fi("cc.in");
ofstream fo("cc.out");
fi >> n;
S = 0; D = 2*n + 1;
for(i = 1; i <= n; i++)
{
C[S][i] = C[n+i][D] = 1;
Cost[S][i] = Cost[n+i][D] = 0;
for(j = 1; j <= n; j++)
{
fi >> Cost[i][n+j];
C[i][n+j] = 1;
Cost[n+j][i] = -Cost[i][n+j];
}
}
while(Bellman())
{
for(i = D; i != S; i = prev[i])
C[prev[i]][i]--, C[i][prev[i]]++;
sol += Dist[D];
}
fo << sol << "\n";
return 0;
}