Pagini recente » Cod sursa (job #1568298) | Cod sursa (job #3198333) | Cod sursa (job #155321) | Cod sursa (job #1796424) | Cod sursa (job #1899417)
#include <bits/stdc++.h>
#define NMAX 201
#define KMAX 16
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
short n,m,k,x,y,z;
int stareFinala;
struct Edge{
short x;
int cost;
};
vector<Edge> g[NMAX];
bitset<NMAX> isK;
int dp[(1<<KMAX)][NMAX];
bool inMatrice[(1<<KMAX)][NMAX];
queue<pair<int,int> >Q;
void bellman() {
while(Q.size()) {
int stareActuala = Q.front().first;
int nodCurent = Q.front().second;
Q.pop();
inMatrice[stareActuala][nodCurent]=0;
for(auto q:g[nodCurent]) {
if(isK[q.x]) {
int stareUrm = (stareActuala|(1<<q.x));
if(dp[stareActuala][nodCurent]+q.cost<dp[stareUrm][q.x]) {
dp[stareUrm][q.x]=dp[stareActuala][nodCurent]+q.cost;
if(!inMatrice[stareUrm][q.x]) {
inMatrice[stareUrm][q.x]=1;
Q.push(make_pair(stareUrm,q.x));
}
}
}
else {
if(dp[stareActuala][nodCurent]+q.cost<dp[stareActuala][q.x]) {
dp[stareActuala][q.x]=dp[stareActuala][nodCurent]+q.cost;
if(!inMatrice[stareActuala][q.x]) {
inMatrice[stareActuala][q.x]=1;
Q.push(make_pair(stareActuala,q.x));
}
}
}
}
}
}
int main()
{
fin>>n>>m>>k;
for(int i=1;i<=k;i++) {
fin>>x;
stareFinala |= (1<<x);
isK[x]=1;
}
for(int i=1;i<=m;i++) {
fin>>x>>y>>z;
g[x].push_back({y,z});
g[y].push_back({x,z});
}
for(int i=0;i<=(1<<KMAX);i++) for(int j=1;j<=n;j++) dp[i][j]=INT_MAX;
dp[0][1]=0;
Q.push(make_pair(0,1));
inMatrice[0][1]=1;
bellman();
int best=INT_MAX;
for(int i=1;i<n;i++) {
for(auto q:g[i]) {
if(q.x==n&&dp[stareFinala][i]!=INT_MAX) {
best=min(best,dp[stareFinala][i]+q.cost);
}
}
}
fout<<best;
return 0;
}