Cod sursa(job #2713291)

Utilizator Iulia25Hosu Iulia Iulia25 Data 27 februarie 2021 17:14:29
Problema Ubuntzei Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <fstream>
#include <bitset>
#include <queue>
#include <vector>

using namespace std;

ifstream cin ("ubuntzei.in");
ofstream cout ("ubuntzei.out");

bitset <32768> viz[2002];

int dp[2002][32768];

struct idk  {
	int cost, nod, stare;
};

auto cmp = [](idk x, idk y)  {
	return (x.cost > y.cost);
};

priority_queue <idk, vector <idk>, decltype(cmp)> pq(cmp);
vector <pair <int, int> > v[2002];

int n, k;
int loc[2002];

void dijkstra()  {
	int cost = 0, nod = 1, stare = loc[1];
	pq.push({cost, nod, stare});
	while (!pq.empty())  {
		cost = pq.top().cost;
		nod = pq.top().nod;
		stare = pq.top().stare;
		pq.pop();
//		if (viz[nod][stare])
//      continue;
//		viz[nod][stare] = 1;
//		cout << cost << ' ' << nod << ' ' << stare << '\n';
		if (stare == (1 << k) - 1 && nod == n)  {
			cout << cost;
			exit(0);
		}
		for (auto it : v[nod])  {
			if (loc[it.first])  {
				int stare2 = (stare | loc[it.first]);
				if (viz[it.first][stare2] && dp[it.first][stare2] <= cost + it.second)
					continue;
				pq.push({cost + it.second, it.first, stare2});
				viz[it.first][stare2] = 1;
				dp[it.first][stare2] = cost + it.second;
			} else  {
				if (viz[it.first][stare] && dp[it.first][stare] <= cost + it.second)
					continue;
				pq.push({cost + it.second, it.first, stare});
				viz[it.first][stare] = 1;
				dp[it.first][stare] = cost + it.second;
			}
		}
	}
}

int main()  {
	int m, x, y, C;
	cin >> n >> m >> k;
	for (int i = 1; i <= k; ++i)  {
		cin >> x;
		loc[x] = (1 << (i - 1));
	}
	for (int i = 1; i <= m; ++i)  {
		cin >> x >> y >> C;
		v[x].push_back({y, C});
		v[y].push_back({x, C});
	}
	dijkstra();
	return 0;
}