Cod sursa(job #2222043)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 16 iulie 2018 13:17:47
Problema Ubuntzei Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

int N,M;
int K;

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

bool L [2002];

struct Stare
{
  int nod;
  int dist;
  int viz; /// BITI CARE VOR REPREZENTA LOCALITATILE VIZITATE
  int nr_viz; /// NR LOCALITATILOR VIZITATE DIN CELE OBLIGATORII
};

struct comp
{
  bool operator()( Stare & A, Stare & B )
  {
    return  ( A.dist > B.dist ) || ( A.dist == B.dist && A.nr_viz < B.nr_viz );
  }
};

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

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

  int tmp;

  for( int i = 1; i <= K; ++i )
  {
     fin >> tmp;
     L[tmp] = 1;
  }

  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 Do()
{
  Stare tmp;

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

  Heap.push( tmp );

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

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

    Heap.pop();

    //fout << n_curent << ' ' << d_curent << ' ' << nr_viz_curent << '\n';
    //for( int i = 1; i <= N; ++i )
    //  fout << bool( viz_curent & ( 1 << i ) ) << ' ';
    //fout << '\n';

    if( n_curent == N && nr_viz_curent == K )
    {
       fout << d_curent << '\n';

       return;
    }

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

       int shift = 1 << w;
       bool rez = viz_temp & shift;

       if( rez == 0 )
       {
         viz_temp = viz_temp | shift;
         if( L[w] ) ++nr_viz_temp;
       }

       tmp.nod = w;
       tmp.dist = d_curent + Cost[n_curent][i];
       tmp.viz = viz_temp;
       tmp.nr_viz = nr_viz_temp;

       Heap.push( tmp );
     }
  }

}

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

    return 0;
}