Pagini recente » Cod sursa (job #1701715) | Cod sursa (job #1786418) | Cod sursa (job #2344184) | Cod sursa (job #1832218) | Cod sursa (job #2564858)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define Mmax 2003
#define Nmax 18
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n,m,v[Nmax],dist[Mmax],dp[(1<<Nmax)][Nmax],k;
struct ceva{
int nod,cost;
bool operator <(const ceva &val)const{
return cost>val.cost;
}
};
vector <ceva> G[Mmax],G1[Mmax];
priority_queue <ceva> q;
void dijkstra(int start)
{
memset(dist,INF,sizeof dist);
q.push({start,0});
dist[start]=0;
while (!q.empty())
{
int x=q.top().nod;
int y=q.top().cost;
q.pop();
if (y!=dist[x]) continue;
for (auto i:G[x])
{
if (y+i.cost<dist[i.nod])
{
dist[i.nod]=y+i.cost;
q.push({i.nod,dist[i.nod]});
}
}
}
}
void rezolvare()
{
int stare,i,j;
for (stare=1;stare<=((1<<k)-1);stare++)
{
for (i=0;i<k;i++)
{
if (((1<<i)&stare)>0 && dp[stare][i]!=INF)
{
for (auto j:G1[i])
{
if (((1<<j.nod)&stare)==0)
{
dp[stare+(1<<j.nod)][j.nod]=min(dp[stare+(1<<j.nod)][j.nod],dp[stare][i]+j.cost);
}
}
}
}
}
}
int main()
{
int i,j,a,b,c;
fin>>n>>m;
fin>>k;
v[0]=1;
for (i=1;i<=k;i++)
fin>>v[i];
k++;
v[k]=n;
for (i=1;i<=m;i++){
fin>>a>>b>>c;
G[a].push_back({b,c});
G[b].push_back({a,c});
}
for (i=0;i<=k;i++)
{
dijkstra(v[i]);
for (int j=0;j<=k;j++)
{
if (i!=j)
G1[i].push_back({j,dist[v[j]]});
}
}
k++;
memset(dp,INF,sizeof dp);
dp[1][0]=0;
rezolvare();
fout<<dp[(1<<k)-1][k-1];
return 0;
}