Cod sursa(job #2924584)

Utilizator apocal1ps13Stefan Oprea Antoniu apocal1ps13 Data 5 octombrie 2022 18:30:05
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#define INF 0x3F3F3F3F
#define NMAX 2000
std::ifstream in("ubuntzei.in");
std::ofstream out("ubuntzei.out");
using namespace std;
int n, m, k;
int tr[20], ad[20][20];
vector<pair<int, int>>g[NMAX + 1];
vector<int>d;
void dijkstra(int node) {
	d = vector<int>(n + 1, INF);
	queue<pair<int, int>>coada;
	d[node] = 0;
	coada.push(make_pair(0, node));
	while (!coada.empty()) {
		int node = coada.front().second;
		int cost = coada.front().first;
		coada.pop();
		if (cost > d[node]) continue;
		for (auto i : g[node]) {
			if (i.second + d[node] < d[i.first]) {
				d[i.first] = d[node] + i.second;
				coada.push(make_pair(d[i.first], i.first));
			}
		}
	}
}
void make_matrix() {
	for (int i = 0; i <= k + 1; i++)
		for (int j = 1; j <= k + 1; j++) ad[i][j] = INF;
	for (int i = 0; i <= k + 1; i++) {
		dijkstra(tr[i]);
		for (int j = 0; j <= k + 1; j++)
			ad[i][j] = ad[j][i] = d[tr[j]];
	}
}
int main() {
	in >> n >> m >> k;
	tr[0] = 1, tr[k + 1] = n;
	for (int i = 1; i <= k; i++) in >> tr[i];
	while (m--) {
		int u, v, c;
		in >> u >> v >> c;
		g[u].push_back(make_pair(v, c));
		g[v].push_back(make_pair(u, c));
	}
	make_matrix();
	n = k + 2;
	vector<vector<int>>dp(1 << n, vector<int>(n, INF));
	dp[1][0] = 0;
	for (int mask = 3; mask < (1 << n); mask += 2) {
		for (int i = 1; i < n; i++)
			if (mask & (1 << i))
				for (int j = 0; j < n; j++)
					if (ad[j][i])
						dp[mask][i] = min(dp[mask][i], dp[mask ^ (1 << i)][j] + ad[j][i]);
	}
	out << dp[(1 << n) - 1][n - 1];
	return 0;
}