Cod sursa(job #2030588)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 1 octombrie 2017 19:54:26
Problema Ubuntzei Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

int n , m , p , t[2001] , mat[2001][2001] , pp = 0 ;

void citire ()
{
    ifstream fin ("ubuntzei.in") ;
    int i , j , x , y , c ;
    fin >> n >> m ;
    fin >> p ;
    for ( i = 1 ; i <= p ; i++ )
    {
        fin >> c ;
        if ( c != 1 && c != n )
        {
            pp++ ;
            t[pp] = c ;
        }
    }
    for ( i = 1 ; i <= n ; i++ )
        for ( j = 1 ; j <= n ; j++ )
            mat[i][j] = 2000000;
    for ( i = 1 ; i <= m ; i++ )
    {
        fin >> x >> y >> c ;
        mat[x][y] = c ;
        mat[y][x] = c ;
    }
    for ( i = 1 ; i <= n ; i++ )
        mat[i][i] = 0 ;
}

int min2( int a , int b )
{
    return a > b ? b : a ;
}

int main()
{
    citire() ;
    ofstream fout("ubuntzei.out") ;
    int i , j , k ;
    long long s , minim = 2000000 ;
    for ( i = 1 ; i <= n ; i++ )
        for ( j = 1 ; j <= n ; j++ )
            for ( k = 1 ; k <= n ; k++ )
                mat[j][k] = min2(mat[j][i]+mat[i][k],mat[j][k]) ;
    sort(t+1,t+pp+1) ;
    do
    {
        s = mat[1][t[1]] ;
        for ( i = 1 ; i < pp ; i++ )
            s = s + mat[t[i]][t[i+1]] ;
        s = s + mat[t[pp]][n] ;
        if ( s < minim )
            minim = s ;
    }while(next_permutation(t+1,t+pp+1)) ;
    fout << minim ;
}