Cod sursa(job #2392317)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 29 martie 2019 21:25:40
Problema Cc Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#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 ;
bool inq [ NR ] ;
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() ;
        inq [ nod ] = 0 ;
        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 ] ;
                if ( !inq [ *it ] )
                q.push( *it ) , inq [ *it ] = 1 ;
                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 ;
}