Pagini recente » Cod sursa (job #2824752) | Cod sursa (job #1008804) | Cod sursa (job #2105234) | Cod sursa (job #1343031) | Cod sursa (job #2675805)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
int n,m,k,orase[17],dist[200+5][1024*32+5];
bool este[200+5],loc[200+5];
const int inf=2000000005;
vector< pair<int,int> > vecini[2005];
multiset< pair< int,pair<int,int> > >coada;
int main()
{
f>>n>>m;
f>>k;
for(int i=1; i<=k; i++) f>>orase[i];
sort(orase+1,orase+k+1);
for(int i=1; i<=k; i++) este[orase[i]]=1,loc[orase[i]]=i;
for(int i=1; i<=m; i++)
{
int x,y,z;
f>>x>>y>>z;
vecini[x].push_back( make_pair(y,z) );
vecini[y].push_back( make_pair(x,z) );
}
if( n<=200 )
{
for(int i=1; i<=200; i++)
for(int j=0; j<=1024*32; j++)
dist[i][j]=inf;
dist[1][0]=0;
coada.insert(make_pair( 0,make_pair(0,1) ) );
while( !coada.empty() )
{
pair<int,int> pereche=coada.begin()->second;
int nod=pereche.second;
int tip=pereche.first;
coada.erase(coada.begin());
for(int i=0;i<vecini[nod].size();i++)
{
int x=vecini[nod][i].first;
int dif=vecini[nod][i].second;
if( !loc[x] ){
if( dist[x][tip]>dist[nod][tip]+dif ){
coada.erase(make_pair( dist[x][tip],make_pair(tip,x) ));
dist[x][tip]=dist[nod][tip]+dif;
coada.insert(make_pair( dist[x][tip],make_pair(tip,x) ));
}
}
else if( !tip&(1>>(loc[x]-1)) ){
if( dist[x][tip+(1>>(loc[x]-1))]>dist[nod][tip]+dif ){
coada.erase(make_pair( dist[x][tip+(1>>(loc[x]-1))],make_pair(tip+(1>>(loc[x]-1)),x) ));
dist[x][tip+(1>>(loc[x]-1))]=dist[nod][tip]+dif;
coada.insert(make_pair( dist[x][tip+(1>>(loc[x]-1))],make_pair(tip+(1>>(loc[x]-1)),x) ));
}
}
}
}
g<<dist[n][(1<<k)-1];
}
}