Cod sursa(job #2223915)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 22 iulie 2018 11:41:23
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <fstream>
#include <queue>
#include <vector>
#define INF 99999999

using namespace std;

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

int N,M;
int K,imp[16];
int mat[2002][ 65600 ];

vector < int > Ad[2001];
vector < int > Cost[2001];

struct Stare
{
  int nod;
  int dist;
  int viz;
};

struct comp
{
  bool operator() ( const Stare & A, const Stare & B )
  {
    return A.dist < B.dist;
  }
};

priority_queue < Stare, vector < Stare >, comp > Heap;

void Read()
{
  fin >> N >> M;

  fin >> K;

  int temp;

  for( int i = 1; i <= K; ++i )
    {
       fin >> temp;

       imp[temp] = i;
    }

  int a,b,d;

  for( int i = 1; i <= M; ++i )
  {
    fin >> a >> b >> d;

    Ad[a].push_back( b );
    Cost[a].push_back( d );

    Ad[b].push_back( a );
    Cost[b].push_back( d );
  }

  fin.close();
}

void Bord()
{
  int limit = 1 << ( K + 1 );

  for( int i = 1; i <= N; ++i )
    for( int j = 0; j <= limit; ++j )
     mat[i][j] = INF;
}

void Do()
{
  Bord();

  Stare tmp;

  tmp.nod = 1;
  tmp.dist = 0;
  tmp.viz = 0;

  mat[1][0] = 0;

  Heap.push( tmp );

  int n_curent,d_curent,viz_curent;
  int w,viz_temp;

  while( ! Heap.empty() )
  {
    n_curent = Heap.top().nod;
    d_curent = Heap.top().dist;
    viz_curent = Heap.top().viz;

    Heap.pop();

    for( int i = 0; i < Ad[n_curent].size(); ++i )
     {
       w = Ad[n_curent][i];

       viz_temp = viz_curent;
       if( imp[w] > 0 ) viz_temp = viz_temp | ( 1 << imp[w] );

       if( mat[w][viz_temp] > d_curent + Cost[n_curent][i] )
       {
         mat[w][viz_temp] = d_curent + Cost[n_curent][i];

         tmp.nod = w;
         tmp.dist = mat[w][viz_temp];
         tmp.viz = viz_temp;

         Heap.push( tmp );
       }
     }
  }
}

void Print()
{
  int limit = 1 << ( K + 1 );

  /*fout << limit << '\n';

  for( int i = 1; i <= N; ++i )
    { for( int j = 0; j <= limit; ++j )
       fout << mat[i][j] << ' ';

      fout << '\n';
    }
*/
  //fout << '\n';

  //for( int i = 1; i <= N; ++i )
   // fout << imp[i] << ' ';

 // fout << sizeof( mat ) / 1024.0 / 1024.0 << '\n';

  fout << mat[N][ limit - 2 ] << '\n';
}

int main()
{
    Read();
    Do();
    Print();

    return 0;
}