Cod sursa(job #675191)

Utilizator thesilverhand13FII Florea Toma Eduard thesilverhand13 Data 7 februarie 2012 13:35:38
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb

 # include <fstream>
 # include <vector>
 # include <queue>
 # include <set>
 
 # define dim 2005
 # define pb push_back
 # define inf 999999
 
 using namespace std;
 
 ifstream f("ubuntzei.in");
 ofstream g("ubuntzei.out");
 
 struct ubuntu
 {
	 int nod, cost;
 };
 
  vector < ubuntu > a[ dim ];
  int n, m, k;
  set < int > b;
  int sol, solmax = 99999999;
  bool viz[ dim ];
  
 inline void citire()
  {
	  int i, x, y, z;
	  
	  f >> n >> m >> k;
	  for ( i = 1 ; i <= k ; i++ )
	  {
		  f >> x;
		  b.insert( x );
	  }
	  b.erase( 1 );
	  
	  for ( i = 1 ; i <= m ; i++ )
	  {
		  f >> x >> y >> z;
		  a[ x ].pb( ( ubuntu ) { y, z } );
		  a[ y ].pb( ( ubuntu ) { x, z } );
	  }
  }
  
 inline void back( int x )
  {
	  int i;
	  if ( x == n && b.size() == 0 )
	  {
		  if ( sol < solmax )
			  solmax = sol;
	  }
	  else
	  for ( i = 0 ; i < a[ x ].size() ; i++ )
		  if ( viz[ a[ x ][ i ].nod ] == 0 )
		  {
			  viz[ a[ x ][ i ].nod ] = 1;
			  sol = sol + a[ x ][ i ].cost;
		  
			  set < int > :: iterator it;
		  
			  it = b.find( a[ x ][ i ].nod );
			 
			  if ( *it != 0 )
				  b.erase( a[ x ][ i ].nod );
		  
			  back( a[ x ][ i ].nod );
		  
			  if ( *it != 0 )
				  b.insert( a[ x ][ i ].nod );
		  
			  sol = sol - a[ x ][ i ].cost;
			  viz[ a[ x ][ i ].nod ] = 0;
		  }
  }
 
  int main()
  {
	  citire();
	  back( 1 );
	  g << solmax;
 	  return 0;
  }