Pagini recente » Cod sursa (job #1858078) | Cod sursa (job #1163451) | Cod sursa (job #1573424) | Cod sursa (job #1870039) | Cod sursa (job #1098683)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int Nmax = 2001;
const int Kmax = 16;
const int inf = 1000000000;
struct node
{
int nod;
int state;
node( int a, int b )
{
nod = a;
state = b;
}
};
vector < pair<int,int> > G[Nmax];
queue <node> Q;
int dist[Nmax][Kmax];
int v[Kmax];
int N, M, K;
int final_state;
void Dijkstra()
{
for ( int i = 1; i <= N; ++i )
for ( int j = 0; j < Kmax; ++j )
dist[i][j] = inf;
dist[1][0] = 0;
Q.push( node( 1, 0 ) );
while ( Q.size() )
{
node n = Q.front();
Q.pop();
for ( auto x: G[n.nod] )
{
for ( int i = 0; i <= K; ++i )
{
if ( i != 0 && v[i] != ( n.nod - 1 ) ) continue;
if ( dist[x.first][n.state | v[i]] > dist[n.nod][n.state] + x.second )
{
dist[x.first][n.state | v[i]] = dist[n.nod][n.state] + x.second;
Q.push( node( x.first, n.state | v[i] ) );
}
}
}
}
ofstream g("ubuntzei.out");
g << dist[N][final_state];
}
int main()
{
ifstream f("ubuntzei.in");
f >> N >> M >> K;
for ( int i = 1; i <= K; ++i )
{
f >> v[i];
v[i]--;
final_state |= ( v[i] );
}
for ( int i = 1, a, b, c; i <= M; ++i )
{
f >> a >> b >> c;
G[a].push_back( make_pair( b, c ) );
G[b].push_back( make_pair( a, c ) );
}
Dijkstra();
return 0;
}