Pagini recente » Statistici Amariei Morosac Ojoc (UAIC_Amariei_Morosac_Ojoc) | Cod sursa (job #2345431) | Cod sursa (job #698563) | Cod sursa (job #751648) | Cod sursa (job #2282001)
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std;
const int maxdim = 10005;
const int maxn = 2005;
vector <pair <int, int> > v[maxn];
int dp[18][(1 << 18)];
int drum[maxn][maxn];
int ubuntel[maxn];
int n;
class cmp
{
public:
bool operator()( pair<int, int> a, pair<int, int> b )
{
return a.second > b.second;
}
};
priority_queue<pair<int, int>, vector< pair<int, int> >, cmp> pq;
void drum_min(int x)
{
drum[ubuntel[x]][x] = 0;
pq.push(make_pair(ubuntel[x], 0));
while(!pq.empty())
{
pair<int, int> p = pq.top();
pq.pop();
/*
if(p.second > drum[p.first])
continue;
*/
if(p.second == drum[p.first][x])
{
for(auto it : v[p.first])
{
if(drum[it.first][x] > it.second + p.second)
{
drum[it.first][x] = it.second + p.second;
pq.push(make_pair(it.first, it.second + p.second));
}
}
}
}
}
char T[maxdim];
int poz = 0;
void cit(int &nr)
{
nr = 0;
while(!isdigit(T[poz]))
{
if(++poz == maxdim)
{
fread(T, 1, maxdim, stdin);
poz = 0;
}
}
while(isdigit(T[poz]))
{
nr = nr * 10 + T[poz] - '0';
if(++poz == maxdim)
{
fread(T, 1, maxdim, stdin);
poz = 0;
}
}
}
int main()
{
freopen("ubuntzei.in", "r", stdin);
freopen("ubuntzei.out", "w", stdout);
int m, k;
cit(n);
cit(m);
cit(k);
ubuntel[1] = 1;
k++;
for(int i = 2; i <= k; i++)
cit(ubuntel[i]);
ubuntel[++k] = n;
for(int i = 1; i <= m; i++)
{
int x, y, z;
cit(x);
cit(y);
cit(z);
v[x].push_back(make_pair(y, z));
v[y].push_back(make_pair(x, z));
}
for(int i = 1; i <= n; i++)
for(int j = 0; j <= k; j++)
drum[i][j] = (1 << 30);
for(int i = 0; i <= k; i++)
for(int j = 0; j <= (1 << k); j++)
dp[i][j] = (1 << 30);
dp[1][1] = 0;
for(int i = 1; i <= k; i++)
drum_min(i);
for(int conf = 0; conf < (1 << k); conf++)
{
for(int i = 0; i < k; i++)
{
if(conf & (1 << i))
continue;
for(int j = 0; j < k; j++)
if(i != j && (conf & (1 << j)))
dp[i + 1][conf + (1 << i)] = min(dp[i + 1][conf + (1 << i)], dp[j + 1][conf] + drum[ubuntel[i + 1]][j + 1]);
}
}
printf("%d\n", dp[k][(1 << k) - 1]);
return 0;
}