Pagini recente » Cod sursa (job #1983487) | Cod sursa (job #863469) | Cod sursa (job #2235861) | Cod sursa (job #2756447) | Cod sursa (job #2237844)
#include <cstdio>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;
int n;
const int INF = 0x7fffffff;
struct Muchie {
int v;
int cost;
};
vector<Muchie>muchii[2005];
int distanta[2005];
int d[20][20];
bool viz[20][20][20];
int mat[20][20];
int ubuntzei[20];
struct Nod {
int index;
int dist;
bool operator <(const Nod &other) const {
return this->dist<other.dist;
}
};
void dijkstra(int s) {
for(int u=1; u<=n; u++)
distanta[u]=INF;
distanta[s]=0;
priority_queue<Nod> q;
q.push({s, 0});
while(!q.empty()){
int u=q.top().index;
if(distanta[u]==q.top().dist){
q.pop();
for(Muchie e : muchii[u]){
if(distanta[e.v]>distanta[u]+e.cost){
distanta[e.v]=distanta[u]+e.cost;
q.push({e.v, distanta[e.v]});
}
}
} else
q.pop();
}
}
int main()
{ freopen("ubuntzei.in", "r",stdin);
freopen("ubuntzei.out", "w",stdout);
int m,k,i,x,y,z,j,kk,l,minn;
scanf("%d%d%d", &n, &m, &k);
for(i=1; i<=k; i++)
scanf("%d", &ubuntzei[i]);
ubuntzei[++k]=1;
for(i=1; i<=m; i++){
scanf("%d%d%d", &x, &y, &z);
muchii[x].push_back({y,z});
muchii[y].push_back({x,z});
}
for(i=1; i<=k; i++){
dijkstra(ubuntzei[i]);
for(j=1; j<=k; j++)
mat[ubuntzei[i]][ubuntzei[j]]=mat[ubuntzei[j]][ubuntzei[i]]=distanta[j];
mat[ubuntzei[i]][n]=distanta[n];
}
viz[1][1][k]=1;
for(i=1; i<k; i++){
d[2][i]+=mat[1][i];
for(j=1; j<=k; j++)
viz[2][i][j]=viz[1][1][j];
viz[2][i][i]=1;
}
for(i=2; i<k; i++){
for(j=1; j<=k; j++){
if(d[i][j]){
for(kk=1; kk<=k; kk++){
if(viz[i][j][kk]==0){
d[i+1][kk]=d[i][j]+mat[j][kk];
for(l=1; l<=k; l++)
viz[i+1][kk][l]=viz[i][j][l];
viz[i+1][kk][kk]=1;
}
}
}
}
}
minn=INF;
for(i=1; i<=k; i++)
if(d[k][i]!=0)
minn=min(minn, d[k][i]+mat[i][n]);
printf("%d", minn);
return 0;
}