Cod sursa(job #2341470)

Utilizator aabbaaaa bbbb aabb Data 11 februarie 2019 20:48:26
Problema Ubuntzei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <iostream>

#include <fstream>

#include <queue>

#include <vector>

#include <cmath>

#include <cstdlib>

#define N 2005

#define pinf 1000000100



using namespace std;







int D[N][3000];

unsigned short int puteri[16]={0,1,2,4,8,16,32,64,128,256, 512,1024,2048,4096,0,0 };



struct Elem

{



  int  dist;

  unsigned short int nod, val_mult;

};



class Compar

{

 public:

 bool operator()( Elem x, Elem y)

 {

    return x.dist>y.dist;



 }

};

priority_queue<  Elem, vector< Elem >, Compar > coada;



int n,m,k, loc[16];

vector <vector< pair< int, int> > >L(N);



ifstream f("ubuntzei.in");

ofstream g("ubuntzei.out");



int apartine(int nod)

{

    for(int i=1;i<=k;i++)

	  if(loc[i]==nod) return i;

	return 0;

}



void dijkstra(int r)

{

  int i,poz,c,j,v,z;

   Elem x;

  for(i=1;i<=n;i++)

     for(j=1;j<3300;j++)

      D[i][j]=pinf;



  x.nod=r; x.dist=0;  x.val_mult=0; //init_mult(x.mult);

  coada.push(x);D[r][0]=0;

  while(!coada.empty())

{

   x=coada.top();

   poz=x.nod; c=x.dist; v=x.val_mult;

   coada.pop();

  if(D[poz][v]==c)

      for(i=0;i<L[poz].size();i++)

      {

         int nod=L[poz][i].first;

         int z=apartine(nod), vv=v;

         if(z )

         { int b=puteri[z];

             if( !(v&b)) //nodul e in mult k

                vv=v+b;

          }

          if(D[nod][vv]>D[poz][v]+L[poz][i].second )

          {

             D[nod][vv]=D[poz][v]+L[poz][i].second;

             x.nod=nod;x.dist=D[nod][vv]; x.val_mult=vv;

             coada.push(x);

           }

       }

    }

}







int main()

{

   int x,y,c,i;

   f>>n>>m;

   f>>k;

   if(k>12) exit(0);

   for(i=1;i<=k;i++)

     f>>loc[i];

   for(i=1;i<=m;i++)

   {

      f>>x>>y>>c;

      L[x].push_back(make_pair(y,c));

      L[y].push_back(make_pair(x,c));

   }



   dijkstra(1);

   int val=0,put=1;

   for(i=1;i<=k;i++)

   {

     val+=put;put*=2;

   }

   g<<D[n][val]<<" ";



   f.close();

   g.close();



   return 0;



}