Pagini recente » Cod sursa (job #2718110) | Cod sursa (job #498480) | Cod sursa (job #2264249) | Cod sursa (job #2187429) | Cod sursa (job #1765096)
#include <bits/stdc++.h>
#define Nmax 2005
#define INF 2000000000
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
struct muchie
{
int nod, cost;
bool operator <(const muchie & A) const
{
return cost > A.cost;
}
};
priority_queue <muchie> q;
vector <muchie> L[Nmax];
int n, k, m;
int dp[20][32800];
int dij[Nmax];
int a[20], cost[20][20];
void Read()
{
muchie w;
int i, x;
fin >> n >> m >> k;
for(i = 1; i <= k; i++)
fin >> a[i];
for(i = 1; i <= m; i++)
{
fin >> x >> w.nod >> w.cost;
L[x].push_back(w);
}
fin.close();
}
void Dijkstra(int nods)
{
int i, j, c, len;
for(i = 1; i <= n; i++)
dij[i] = INF;
dij[nods] = 0;
muchie w, w1;
w.nod = nods;
w.cost = 0;
q.push(w);
while(!q.empty())
{
w = q.top();
len = L[w.nod].size();
q.pop();
for(i = 0; i < len; i++)
{
j = L[w.nod][i].nod;
c = L[w.nod][i].cost;
if(dij[j] > dij[w.nod] + c)
{
dij[j] = dij[w.nod] + c;
w1.nod = j;
w1.cost = dij[j];
q.push(w1);
}
}
}
}
void Solve()
{
int smax, i, j, nod, nod1, nod2, sol, stare;
Dijkstra(1);
smax = (1 << k);
for(i = 1; i <= k; i++)
{
nod = a[i];
dp[nod][1 << i] = dij[nod];
}
for(i = 1; i <= k; i++)
{
nod = a[i];
Dijkstra(nod);
for(j = 1; j <= n; j++)
cost[nod][j] = dij[j];
}
for(stare = 1; stare <= smax; stare++)
for(i = 1; i <= k; i++)
{
nod1 = a[i];
if((stare & (1 << i)))
for(j = 1; j <= k; j++)
{
nod2 = a[j];
if(!(stare & (1 << j)))
dp[nod1][stare | (1 << j)]
= min( dp[nod1][stare | (1 << i)],
dp[nod1][stare] + cost[nod1][nod2]);
}
}
sol = INF;
for(i = 1; i <= k; i++)
{
nod = a[i];
sol = min(sol, dp[nod][smax] + cost[nod][n]);
}
fout << sol;
}
int main()
{
Read();
Solve();
return 0;
}