Pagini recente » Cod sursa (job #1910421) | Cod sursa (job #640431) | Cod sursa (job #1881146) | Cod sursa (job #794705) | Cod sursa (job #2223912)
#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[16][ 1 << 16 ];
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];
//fout << viz_curent << ' ';
viz_temp = viz_curent;
if( imp[w] > 0 ) viz_temp = viz_temp | ( 1 << imp[w] );
//fout << viz_temp << '\n';
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 );
/* 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 << mat[N][ limit - 2 ] << '\n';
}
int main()
{
Read();
Do();
Print();
return 0;
}