Cod sursa(job #2326656)

Utilizator HumikoPostu Alexandru Humiko Data 23 ianuarie 2019 20:24:22
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

int n, m, k;
int cities[16];
int cost[2005][2005];
int dp[32800][16];
vector <pair<int, int>> graph[2005];
static const int INF = 1e9;

class cmp
{
public:
	const bool operator () (const pair <int, int> &a, const pair<int, int> &b)
	{
		return a.second > b.second;
	}
};

void dijkstra(int node)
{
	priority_queue <pair<int, int>, vector<pair<int, int>>, cmp> pq;

	for (int i = 0; i < n; ++i) cost[node][i] = INF;
	cost[node][node] = 0;

	pq.push({ node, 0 });
	while (pq.size())
	{
		int cur = pq.top().first;
		int dist = pq.top().second;
		pq.pop();

		if (cost[node][cur] != dist) continue;

		for (auto x : graph[node])
		{
			if (cost[node][x.first] > cost[node][cur] + x.second)
			{
				cost[node][x.first] = cost[node][cur] + x.second;
				pq.push({ x.first, cost[node][cur] });
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	fin.tie(0); fout.tie(0);

	fin >> n >> m >> k;
	for (int i = 0; i < k; ++i)
	{
		fin >> cities[i];
		cities[i]--;
	}
	
	for (int i = 1; i <= m; ++i)
	{
		int x, y, c;
		fin >> x >> y >> c;
		x--;
		y--;
		graph[x].push_back({ y, c });
		graph[y].push_back({ x, c });
	}
	
	dijkstra(1);
	for (int i = 0; i < k; ++i) dijkstra(cities[i]);
	dijkstra(n - 1);

	int bitmask = (1 << k);

	for (int bit = 2; bit < bitmask; ++bit)
		for (int j = 0; j < k; ++j)
			dp[bit][j] = INF;

	for (int bit = 0; bit < bitmask; ++bit)
		for (int i = 0; i < k; ++i)
			if ((1 << i)&bit)
				for (int j = 0; j < k; ++j)
					dp[bit][i] = min(dp[bit][i],
								     dp[bit ^ (1 << j)][cities[j]] + cost[cities[j]][cities[i]]);

	int ans = INF;
	
	for (int i = 0; i < k; ++i) ans = min(ans, dp[(bitmask - 1)][cities[i]] + cost[cities[i]][n - 1]);

	fout << ans << '\n';
}