Pagini recente » Istoria paginii runda/9_2 | Istoria paginii runda/agora2004/clasament | Istoria paginii runda/oni_gim_2016/clasament | Cod sursa (job #1216297) | Cod sursa (job #1716011)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream fin( "cc.in" ); ofstream fout( "cc.out" );
typedef vector< int > graf;
const int inf = (1 << 30);
const int nodmax = 100;
const int nmax = 2 * nodmax + 2;
int n, S, D;
int d[ nmax + 1 ], t[ nmax + 1 ];
bool in_coada[ nmax + 1 ];
int c[ nmax + 1 ][ nmax + 1 ];
int cost[ nmax + 1 ][ nmax + 1 ];
graf g[ nmax + 1 ];
queue< int > q;
bool bellman_ford() {
for( int i = 1; i <= n; ++ i ) {
d[ i ] = inf;
in_coada[ i ] = 0;
}
d[ S ] = 0;
in_coada[ S ] = 1;
q.push( S );
while ( !q.empty() ) {
int x = q.front();
q.pop();
in_coada[ x ] = 0;
for( auto it : g[ x ] ) {
if ( c[ x ][ it ] > 0 && d[ x ] + cost[ x ][ it ] < d[ it ] ) {
d[ it ] = d[ x ] + cost[ x ][ it ];
t[ it ] = x;
if( in_coada[ it ] == 0 ) {
in_coada[ it ] = 1;
q.push( it );
}
}
}
}
return d[ D ] != inf;
}
int main() {
fin >> n;
for( int i = 1; i <= n; ++ i ) {
for( int j = 1; j <= n; ++ j ) {
fin >> cost[ i ][ j + n ];
cost[ j + n ][ i ] = -cost[ i ][ j + n ];
c[ i ][ j + n ] = 1;
g[ i ].push_back( j + n );
g[ j + n ].push_back( i );
}
}
S = n * 2 + 1; D = n * 2 + 2;
for( int i = 1; i <= n; ++ i ) {
g[ S ].push_back( i );
g[ i ].push_back( S );
c[ S ][ i ] = 1;
}
for( int i = n + 1; i <= 2 * n; ++ i ) {
g[ D ].push_back( i );
g[ i ].push_back( D );
c[ i ][ D ] = 1;
}
n = n * 2 + 2;
int ans = 0;
while ( bellman_ford() ) {
int fluxmin = inf;
for( int x = D; x != S; x = t[ x ] ) {
if ( c[ t[ x ] ][ x ] < fluxmin ) {
fluxmin = c[ t[ x ] ][ x ];
}
}
for( int x = D; x != S; x = t[ x ] ) {
c[ t[ x ] ][ x ] -= fluxmin;
c[ x ][ t[ x ] ] += fluxmin;
ans += cost[ t[ x ] ][ x ] * fluxmin;
}
}
fout << ans << "\n";
fin.close();
fout.close();
return 0;
}