Cod sursa(job #2216534)

Utilizator WebDesignbyTMGhiorghiu Ioan-Viorel WebDesignbyTM Data 27 iunie 2018 09:52:44
Problema Ubuntzei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#define DM 2001
#define DN (1 << 15) + 1
#define inf 0x3f3f3f3f
#include <cstring>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

ifstream fi ("ubuntzei.in");
ofstream fo ("ubuntzei.out");
int n, k, u[16], m, dp[16][DN], loc[DN], a, b, c, dst[17][DM], bst;
struct str
{
	int n, c;
	bool operator < (const str &other) const
	{
		return c > other.c;
	}
} aux;
priority_queue <str> pq;
vector <str> v[DM];

void dijkstra(int x, int y)
{
	memset(dst[y], inf, sizeof dst[y]);
	dst[y][x] = 0;
	pq.push({x, 0});
	while (!pq.empty())
	{
		aux = pq.top();
		pq.pop();
		if (aux.c != dst[y][aux.n])
			continue;
		for (auto i : v[aux.n])
			if (dst[y][i.n] > i.c + aux.c)
			{
				dst[y][i.n] = i.c + aux.c;
				pq.push({i.n, dst[y][i.n]});
			}
	}
}

int calc(int x, int y)
{
	if (dp[y][x+(1<<(y-1))] != inf)
		return dp[y][x+(1<<(y-1))];
	if (x == 0)
		return dst[1][u[y]];
	int rez = inf;
	for (int i = 0; i < k; ++i)
		if (x & (1 << i))
		{
			dp[i+1][x] = calc(x - (1 << i), i+1);
			if (rez > calc(x - (1 << i), i+1) + dst[i+2][u[y]])
				rez = calc(x - (1 << i), i+1) + dst[i+2][u[y]];
		}
	return rez;
}

int main()
{
	//citire
	fi >> n >> m >> k;
	for (int i = 1; i <= k; ++i)
		fi >> u[i];
	for (int i = 1; i <= m; ++i)
	{
		fi >> a >> b >> c;
		v[a].push_back({b, c});
		v[b].push_back({a, c});
	}
	//precalculare
	dijkstra(1, 1);
	for (int i = 1; i <= k; ++i)
		dijkstra(u[i], i + 1);
	if (!k)
	{
		fo << dst[1][n];
		return 0;
	}
	//dinamica
	bst = inf;
	for (int i = 1; i <= k; ++i)
		memset(dp[i], inf, sizeof dp[i]);
	for (int i = 1; i <= k; ++i)
		bst = min(bst, calc((1 << k) - 1 - (1 << (i - 1)), i) + dst[i+1][n]);
	fo << bst;
	return 0;
}