Cod sursa(job #3210679)

Utilizator vladdobro07vladdd vladdobro07 Data 7 martie 2024 03:39:19
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <bits/stdc++.h>
#define pii pair<int,int>

using namespace std;

// Skibidi Pula Skibidi Pula Skibidi Pula Skibidi Pula Skibidi Pula Skibidi Pula Skibidi Pula
//(cred ca innebunesc ajutor ajuta ti ma va rog nu mai suport)

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

const int nmax = 2e3, kmax = 15, inf = 1e9 + 79, maskmax = (1 << (kmax + 2));
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<pii> g[nmax + 1], gmic[kmax + 2];
vector<int> ubu(kmax + 2), minn(nmax + 1, inf);
vector<bool> a(nmax + 1, 0);
int dp[maskmax][kmax + 2];
int n, m, k, x, y, ct;

void read() {
	fin >> n >> m >> k;

	ubu.push_back(1);
	for(int i = 0; i < k; i++) {
		fin >> x;
		ubu.push_back(x);
	}
	ubu.push_back(n);

	for(int i = 0; i < m; i++) {
		fin >> x >> y >> ct;
		g[x].push_back({y, ct});
		g[y].push_back({x, ct});
	}
}

void make_djikstra() {
	pq.push({0, 1});
	minn[1] = 0;

	while(!pq.empty()) {
		int cur_nod = pq.top().second, cur_cost = pq.top().first;
		pq.pop();

		if(a[cur_nod] == 1)
			continue;
		a[cur_nod] = 1;

		for(auto muchie : g[cur_nod]) {
			int n_nod = muchie.first, n_cost = cur_cost + muchie.second;

			if(minn[n_nod] > n_cost) {
				pq.push({n_cost, n_nod});
				minn[n_nod] = n_cost;
			}
		}
	}
}

void make_smol_graph() {
	// 1 ubu[1] ubu[2] ... ubu[k] n -> 0 1 2 .. k+1
        // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA SCHIMBA DIJKSTRA UL CA NU MERGE MOMENTAN dar mi e somn honk mimimi
}

void hamilton() {
	for(int i = 0; i < k+2; i++)
		for(int j = 0; j < (1 << (k+2)); j++)
			dp[j][i] = inf;
	dp[1][0] = 0;

	for(int mask = 1; mask < (1 << (k+2)); mask++) {
		for(int ubuntzel = 0; ubuntzel < k+2; ubuntzel++) {
			if(((1 << ubuntzel) & mask)) {
				int cost = dp[mask][ubuntzel], nnode, ncost;

				for(auto [nnode, ncost] : gmic[ubuntzel]) {
					if(!((1 << nnode) & mask))
						dp[(mask | (1 << nnode))][nnode] = min(dp[(mask | (1 << nnode))][nnode], cost + ncost);
				}
			}
		}
	}
}

void print() {
	int rez = inf, allmask = (1 << (k + 2)) - 1;

	for(int i = 0; i < k + 2; i++)
		rez = min(rez, dp[allmask][i] + get_cost(ubu[i], n));

	fout << rez << "\n";
}

int main() {
	read();
	make_djikstra();
	make_smol_graph();
	hamilton();
	print();
	return 0;
}