Pagini recente » Cod sursa (job #2687950) | Cod sursa (job #116769) | Cod sursa (job #3170939) | Cod sursa (job #181276) | Cod sursa (job #2990172)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
struct muchie
{
int node, cost;
};
struct heap
{
int node, cost;
bool operator < (const heap &num) const
{
return cost > num.cost;
}
};
const int NMAX = 2005, INF = 1e9;
int n, m, k, ans = INT_MAX;
bool frq[NMAX];
int adj[25][NMAX];
int loca[NMAX], arr[NMAX];
vector<muchie>g[NMAX];
inline void dijkstra(int start, int poz)
{
int dist[NMAX];
priority_queue<heap>pq;
pq.push({start, 0});
adj[poz][start] = 0;
while(!pq.empty())
{
int curr_node = pq.top().node;
pq.pop();
for(auto new_node : g[curr_node])
{
if(adj[poz][new_node.node] > adj[poz][curr_node] + new_node.cost)
{
adj[poz][new_node.node] = adj[poz][curr_node] + new_node.cost;
pq.push({new_node.node, adj[poz][new_node.node]});
}
}
}
}
inline void backtracking(int poz)
{
if(poz == k + 1)
{
int s = 0;
s += adj[arr[1]][1];
s += adj[arr[k]][n];
for(int i = 1; i < k; ++ i)
{
s+= adj[arr[i]][arr[i+1]];
}
ans = min(ans, s);
}
else
{
for(int i = 1; i <= k; ++ i)
{
if(!frq[i])
{
frq[i] = 1;
arr[poz] = i;
backtracking(poz + 1);
frq[i] = 0;
}
}
}
}
int main()
{
fin >> n >> m >> k;
for(int i = 1; i <= k; ++ i)
fin >> loca[i];
for(int i = 1; i <= m; ++ i)
{
int x, y, cost;
cin >> x >> y >> cost;
g[x].push_back({y, cost});
g[y].push_back({x, cost});
}
//for(int i = 0; i < 25; ++ i)
//for(int j = 0; j < NMAX; ++ j)
//adj[i][j] = INF;
for(int i = 1; i <= k; ++ i)
{
dijkstra(loca[i], i);
}
backtracking(1);
fout << ans;
}