Cod sursa(job #2118784)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 30 ianuarie 2018 23:51:04
Problema Bibel Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <bits/stdc++.h>
#define INF 100000

using namespace std;

ifstream fin("bibel.in") ;
ofstream fout("bibel.out") ;

vector<pair<int,int> > graf[20] ;
vector<pair<int,int> > v ;
int n ;
int dp[1<<18][18] ;

void calc(int x,int y)
{
    int i;
    for ( i = 0 ; i < n ; i++ )
    {
        graf[i].push_back(make_pair(n,((v[i].first-x)*(v[i].first-x)+(v[i].second-y)*(v[i].second-y)))) ;
        //cout << " am pus in muchie intre " << i << " " << n << " " << graf[i][graf[i].size()-1].second << endl ;
        graf[n].push_back(make_pair(i,((v[i].first-x)*(v[i].first-x)+(v[i].second-y)*(v[i].second-y)))) ;
    }
}

void hamilton()
{
    int i , j , sol , k , vecin ;
    for ( i = 0 ; i < (1<<(1+n)) ; i++ )
        for ( j = 0 ; j <= n ; j++ )
            dp[i][j] = INF ;
    for ( i = 0 ; i <= n ; i++ )
        dp[1<<i][i] = 0 ;
    dp[1][0] = 0 ;
    for ( i = 1 ; i < (1<<(1+n)) ; i++ )
    {
        for ( j = 0 ; j <= n ; j++ )
        {
            if ( (i&(1<<j)) != 0 )
            {
                for ( k = 0 ; k < graf[j].size() ; k++ )
                {
                    vecin = graf[j][k].first ;
                    if ( (i&(1<<vecin)) == 0 )
                        dp[i|(1<<vecin)][vecin] = min(dp[i][j]+graf[j][k].second,dp[i|(1<<vecin)][vecin]) ;
                }
            }
        }
    }
    sol = dp[(1<<(1+n))-1][0] ;
    fout << sol << '\n';
}

int main()
{
    int p , x , y ;
    v.push_back(make_pair(0,0)) ;
    n = 0 ;
    while(!fin.eof())
    {
        p = 2;
        fin >> p ;
        if ( p == 0 )
        {
            n++ ;
            fin >> x >> y ;
            v.push_back(make_pair(x,y)) ;
            calc(x,y) ;
        }
        else if ( p == 1 )
            hamilton() ;
    }
}