Pagini recente » Istoria paginii utilizator/stevejobs | Cod sursa (job #2072283) | Cod sursa (job #2141012) | Profil Pojo | Cod sursa (job #2481738)
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
#define inf INT_MAX
using namespace std;
int n, m, i, j, k, x, y, dist, p, nod, cost, nd, nv;
int d[18][2002], dp[18][32768], v[18];
bool viz[2002];
vector < pair <int, int> > g[2001];
ifstream fin ("ubuntzei.in");
ofstream fout ("ubuntzei.out");
struct cmp {
bool operator() (pair <int, int> &x, pair <int, int> &y) const {
return x.second>y.second;
}
};
priority_queue < pair <int, int> , vector < pair <int, int> > , cmp> q;
void dijkstra (int s)
{
int nod=v[s];
for(int i=1;i<=n;i++)
{
viz[i]=0;
d[s][i]=inf;
}
d[s][nod]=0;
q.push({nod,0});
while (!q.empty()) {
pair <int,int> p=q.top();
if (viz[p.first]==1)
{
q.pop();
continue;
}
nd=p.first;
viz[nd]=1;
for (int i=0;i<g[nd].size();i++) {
nv=g[nd][i].first;
cost=g[nd][i].second;
if (viz[nv]==0 && d[s][nv]>d[s][nd]+cost) {
d[s][nv]=d[s][nd]+cost;
q.push({nv, d[s][nv]});
}
}
}
}
int main () {
fin>>n>>m;
fin>>k;
for (i=1;i<=k;i++) {
fin>>v[i];
}
for (i=1;i<=m;i++) {
fin>>x>>y>>dist;
g[x].push_back({y, dist});
g[y].push_back({x, dist});
}
v[++k]=1;
for (i=1;i<=k;i++) {
dijkstra(i);
}
if(k==1)
{
fout<<d[1][n];
return 0;
}
else
k--;
for (i=1;i<=k;i++) {
for (j=1;j<(1<<k);j++) {
dp[i][j]=inf;
}
dp[i][1<<(i-1)]=d[i][1];
}
for (int st=1;st<(1<<k);st++) {
for (i=1;i<=k;i++) {
if(dp[i][st]!=inf)
for(j=1;j<=k;j++)
if (i!=j && ((st>>(j-1))&1)==0) {
{
int stare=st|(1<<(j-1));
dp[j][stare]=min(dp[j][stare],dp[i][st]+d[i][v[j]]);
}
}
}
}
int sol=inf;
for(i=1;i<=k;i++)
sol=min(sol,dp[i][(1<<k)-1]+d[i][n]);
fout<<sol;
return 0;
}