Pagini recente » Cod sursa (job #1185025) | Cod sursa (job #2722684) | Cod sursa (job #170580) | Cod sursa (job #1664947) | Cod sursa (job #2722158)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
const int INF = 0x3f3f3f3f3f3f3f3f;
struct element{
int node, cost;
bool operator<(const element& other) const{
return cost > other.cost;
}
};
int n, m, k, ans = INF;
int adjMat[2010][2010], loc[20], hamilton[262144][18], dist[2010];
bool fr[2010];
vector<vector<pair<int, int>>> adj;
void buildHamilton()
{
for(int i = 1; i < (1 << (k + 1)); i++)
for(int j = 0; j <= k; j++)
hamilton[i][j] = INF;
for(int i = 0; i <= k; i++)
hamilton[(1 << i)][i] = 0;
for(int i = 1; i < (1 << (k + 1)); i++)
{
for(int j = 0; j <= k; j++)
{
if(i & (1 << j))
{
//cout << "CONFIG: " << i << '\n' << "END: " << j << '\n';
for(int x = 0; x <= k; x++)
{
if(i & (1 << x))
{
//cout << x << ' ' << (i ^ (1 << j)) << ' ' << hamilton[(i ^ (1 << j))][x] << ' ' << adjMat[x][j] << '\n';
hamilton[i][j] = min(hamilton[i][j], hamilton[(i ^ (1 << j))][x] + adjMat[x][j]);
}
}
//cout << hamilton[i][j] << "\n\n";
}
}
}
}
void dijkstra(int start)
{
memset(fr, 0, sizeof fr);
for(int i = 1; i <= n; i++)
dist[i] = INF;
dist[start] = 0;
priority_queue<element> q;
q.push({start, dist[start]});
while(!q.empty())
{
element x = q.top();
q.pop();
if(fr[x.node])
continue;
fr[x.node] = 1;
//cout << x.node << ' ' << x.cost << '\n';
for(auto it: adj[x.node])
{
if(dist[it.first] > x.cost + it.second)
{
dist[it.first] = x.cost + it.second;
q.push({it.first, dist[it.first]});
}
}
}
}
int main()
{
in >> n >> m >> k;
for(int i = 1; i <= k; i++)
in >> loc[i];
loc[0] = 1;
loc[k + 1] = n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i != j)
adjMat[i][j] = INF;
adj.resize(n + 5);
for(int i = 1; i <= m; i++)
{
int x, y, cost;
in >> x >> y >> cost;
adj[x].emplace_back(y, cost);
adj[y].emplace_back(x, cost);
}
for(int i = 0; i <= k + 1; i++)
{
//cout << "START: " << loc[i] << '\n';
dijkstra(loc[i]);
for(int j = 0; j <= k + 1; j++)
{
//cout << i << ' ' << j << ' ' << loc[j] << ' ' << dist[loc[j]] << '\n';
adjMat[i][j] = dist[loc[j]];
}
//cout << '\n';
}
/*
for(int i = 0; i <= k + 1; i++)
{
for(int j = 0; j <= k + 1; j++)
cout << adjMat[i][j] << ' ';
cout << '\n';
}
*/
buildHamilton();
for(int i = 0 ; i <= k; i++)
ans = min(ans, hamilton[(1 << (k + 1)) - 1][i] + adjMat[i][k + 1]);
out << ans << '\n';
return 0;
}