Pagini recente » Cod sursa (job #2039051) | Cod sursa (job #2098258) | Cod sursa (job #2149537) | Cod sursa (job #1500958) | Cod sursa (job #2030031)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n , m , p , t[2001] , mat[2001][2001] ;
void citire ()
{
ifstream fin ("ubuntzei.in") ;
int i , j , x , y , c ;
fin >> n >> m ;
fin >> p ;
for ( i = 1 ; i <= p ; i++ )
fin >> t[i] ;
for ( i = 1 ; i <= n ; i++ )
for ( j = 1 ; j <= n ; j++ )
mat[i][j] = 10000 ;
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 ? a : b ;
}
int main()
{
citire() ;
ofstream fout("ubuntzei.out") ;
int i , j , k , s , minim = 100000 ;
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+p+1) ;
do
{
s = mat[1][t[1]] ;
for ( i = 2 ; i < p ; i++ )
s = s + mat[t[i]][t[i+1]] ;
s = s + mat[t[p]][n] ;
if ( s < minim )
minim = s ;
}while(next_permutation(t+1,t+p+1)) ;
fout << s ;
}