Pagini recente » Profil adriana.ionita | Cod sursa (job #2960892) | Cod sursa (job #2707571) | Rating Radeanu Razvan (radeanurazvan99) | Cod sursa (job #2392314)
#include <bits/stdc++.h>
#define pb push_back
using namespace std ;
const int NR = 205 , oo = ( 1 << 30 ) ;
ifstream in ("cc.in") ;
ofstream out ("cc.out") ;
vector < int > v [ NR ] ;
vector < int > d ( NR ) ;
int n , x , cost [ NR ][ NR ] , cap [ NR ][ NR ] , t [ NR ] ;
int64_t sol ;
struct cmp {
inline bool operator() ( const int i , const int j ) {
return d [ i ] > d [ j ] ;
}
};
priority_queue < int , vector < int > , cmp > q ;
bool dijkstra ( ) {
int nod , i ;
for ( i = 1 ; i <= 2 * n + 1 ; ++ i ) d [ i ] = oo ;
d [ 0 ] = 0 ;
q.push( 0 ) ;
while ( !q.empty() ) {
nod = q.top() ;
q.pop() ;
for ( vector < int > :: iterator it = v [ nod ].begin() ; it != v [ nod ].end() ; ++ it )
if ( cap [ nod ][ *it ] )
if ( d [ nod ] + cost [ nod ][ *it ] >= 0 && d [ nod ] + cost [ nod ][ *it ] < d [ *it ] ) {
d [ *it ] = d [ nod ] + cost [ nod ][ *it ] ;
q.push( *it ) ;
t [ *it ] = nod ;
}
}
if ( d [ n << 1 | 1 ] == oo ) return 0 ;
nod = 2*n + 1 ;
while ( nod ) {
cap [ t [ nod ] ][ nod ] = 0 ;
cap [ nod ][ t [ nod ] ] = 1 ;
nod = t [ nod ] ;
}
sol += d[ n << 1 | 1 ] ;
return 1 ;
}
int main () {
int i , j ;
in >> n ;
for ( i = 1 ; i <= n ; ++ i )
for ( j = n + 1 ; j <= 2 * n ; ++ j ) {
in >> cost [ i ][ j ] ;
cost [ j ][ i ] = -cost [ i ][ j ] ;
v [ i ].pb ( j ) ;
v [ j ].pb ( i ) ;
cap [ i ][ j ] = 1 ;
}
for ( i = 1 ; i <= n ; ++ i ) {
cap [ 0 ][ i ] = 1 ;
v [ 0 ].pb ( i ) ;
cap [ n + i ][ n << 1 | 1 ] = 1 ;
v [ n + i ].pb ( n << 1 | 1 ) ;
}
while ( dijkstra() ) ;
out << sol ;
}