Pagini recente » Cod sursa (job #892843) | Cod sursa (job #674408) | Cod sursa (job #2687400) | Cod sursa (job #2969840) | Cod sursa (job #704469)
Cod sursa(job #704469)
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int KMAX = 18;
const int NMAX = 2005;
const int INF = 1000000005;
int N, M, K;
vector< pair<int, int> > Graf[NMAX];
int vf[KMAX];
int dist[NMAX];
int lng[KMAX][KMAX];
int d[KMAX][(1<<KMAX)];
void citi()
{
scanf("%d%d", &N, &M);
scanf("%d", &K);
for(int i = 1 ; i <= K ; i++)
scanf("%d", &vf[i]);
sort(vf + 1, vf + K + 1);
if(vf[K] != N) vf[++K] = N;
if(vf[1] != 1) vf[++K] = 1;
sort(vf + 1, vf + K + 1);
for(int i = 1 ; i <= M ; i++)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
Graf[x].push_back( make_pair(y, c) );
Graf[y].push_back( make_pair(x, c) );
}
}
void bellmanford(int pct)
{
fill(dist + 1, dist + N + 1, INF);
dist[vf[pct]] = 0;
queue<int> Q;
Q.push(vf[pct]);
while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(vector< pair<int, int> > :: iterator it = Graf[x].begin() ; it != Graf[x].end() ; it++)
if(dist[it->first] > dist[x] + it->second)
{
dist[it->first] = dist[x] + it->second;
Q.push(it->first);
}
}
for(int i = 1; i <= K ; i++)
lng[pct][i] = dist[vf[i]];
}
void dinamic()
{
for(int i = 1 ; i <= K ; i++)
for(int j = 0; j <= (1<<K) - 1 ; j++)
d[i][j] = INF;
d[1][1] = 0;
for(int j = 0; j <= (1<<K) - 1 ; j++)
for(int i = 1 ; i <= K ; i++)
for(int l = 1 ; l <= K ; l++)
if(l != i && j&(1<<(l-1)))
d[i][j] = min(d[i][j], d[l][j - (1<<(i-1))] + lng[l][i]);
}
int main()
{
freopen("ubuntzei.in", "r", stdin);
freopen("ubuntzei.out", "w", stdout);
citi();
for(int i = 1 ; i <= K ; i++)
bellmanford(i);
dinamic();
printf("%d\n", d[K][(1<<K) - 1]);
return 0;
}