Pagini recente » Cod sursa (job #1362410) | Cod sursa (job #2515402) | Cod sursa (job #706454) | Cod sursa (job #2602301) | Cod sursa (job #3287521)
#include <bits/stdc++.h>
#define inf 100000005
using namespace std;
int n,m,k,o[20],mat[20][20],vf[2005],dmin[2005];
int rsp[16][70000];
vector <pair<int,int>> v[2005];
void dijk(int source)
{
dmin[source]=0;
priority_queue <pair<int,int>> pq;
for (auto it : v[source])
{
dmin[it.second]=it.first;
pq.push({-dmin[it.second],it.second});
}
while (!pq.empty())
{
auto u=pq.top();
pq.pop();
if (-u.first>dmin[u.second])
continue;
for (auto it : v[u.second])
{
if (dmin[it.second]>-u.first+it.first)
{
dmin[it.second]=-u.first+it.first;
pq.push({-dmin[it.second],it.second});
}
}
}
}
int main()
{
ifstream f ("ubuntzei.in");
ofstream g ("ubuntzei.out");
f>>n>>m>>k;
for (int i=1; i<=k; i++)
f>>o[i];
k++;
o[0]=1;
o[k]=n;
int x,y,z;
for (int i=1; i<=m; i++)
{
f>>x>>y>>z;
v[x].push_back({z,y});
v[y].push_back({z,x});
}
for (int i=0; i<=k; i++)
{
for (int j=1; j<=n; j++)
vf[j]=0,dmin[j]=inf;
dijk(o[i]);
for (int j=0; j<=k; j++)
mat[i][j]=dmin[o[j]];
}
/// for (int i=0; i<=k; i++)
/// for (int j=0; j<=k; j++)
/// cout<<o[i]<<' '<<o[j]<<' '<<mat[i][j]<<'\n';
///first e costu
///second.first e prin ce a trecut
///second.second e nodu in care e
for (int i=0; i<=15; i++)
for (int j=0; j<=67000; j++)
rsp[i][j]=inf;
if (k==1)
{
g<<mat[0][1];
return 0;
}
priority_queue<pair<int,pair<int,int>>> pq;
k--;
for (int i=1; i<=k; i++)
{
pq.push({-mat[0][i],{1<<(i-1),i}});
rsp[i][1<<(i-1)]=mat[0][i];
}
// cout<<1;
int ct=0;
while (!pq.empty())
{
auto u=pq.top();
pq.pop();
// if (u.second.first==((1<<k)-1))
// break;
//cout<<ct<<' '<<-u.first<<' '<<u.second.first<<' '<<u.second.second<<'\n';
ct++;
if (rsp[u.second.second][u.second.first]==-u.first)
for (int i=0; i<k; i++)
if ((u.second.first&(1<<i))==0)
if (rsp[i+1][u.second.first+(1<<i)]>-u.first+mat[u.second.second][i+1])
{
rsp[i+1][u.second.first+(1<<i)]=-u.first+mat[u.second.second][i+1];
pq.push({-rsp[i+1][u.second.first+(1<<i)],{u.second.first+(1<<i),i+1}});
}
}
//cout<<ct;
int rspp=inf;
for (int i=1; i<=k; i++)
{
//cout<<i<<' '<<(1<<k)-1<<' '<<rsp[i][(1<<k)-1]<<' '<<mat[i][k+1]<<'\n';
rspp=min(rspp,rsp[i][(1<<k)-1]+mat[i][k+1]);
}
g<<rspp;
}