Cod sursa(job #2346981)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 18 februarie 2019 11:58:24
Problema Ubuntzei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 2007;
const int MAX = 1e9;
const int maskD = (1 << 15) + 7;

struct Edge
{
	int x, cost;
};

int pos[DIM];
bool inq[DIM][maskD];
int d[DIM][maskD];

struct cmp
{
	bool operator() (pair <int, int> a, pair <int, int> b)
	{
		return (d[a.first][a.second] > d[b.first][b.second]);
	}
};

priority_queue <pair <int, int>, vector <pair <int, int> >, cmp> q;

vector <Edge> v[DIM];

void dijkstra()
{
	d[1][0] = 0;
	q.push({1, 0});
	inq[1][0] = true;
	
	while(!q.empty())
	{
		int nod = q.top().first;
		int mask = q.top().second;
		
		q.pop();
		
		inq[nod][mask] = false;
		
		for(auto i : v[nod])
		{
			int cost = i.cost;
			int next = i.x;
			
			int newMask = mask;
			
			if(pos[next] != 0)
				newMask |= (1 << pos[next]);
			
			if(d[next][newMask] > d[nod][mask] + cost)
			{
				d[next][newMask] = d[nod][mask] + cost;
				
				if(inq[next][newMask] == false)
				{
					inq[next][newMask] = true;
					q.push({next, newMask});
				}
			}
		}
	}
}

int main()
{
	int n, m, k;
	in >> n >> m >> k;
	
	int mask = 0;
	
	for(int i = 1; i <= k; i++)
	{
		int x;
		in >> x;
		
		pos[x] = i;
		
		mask ^= (1 << i);
	}
	
	while(m--)
	{
		int x, y, c;
		in >> x >> y >> c;
		
		v[x].push_back({y, c});
		v[y].push_back({x, c});
	}
	
	for(int i = 1; i <= n; i++)
		for(int j = 0; j <= mask; j++)
			d[i][j] = MAX;
	
	dijkstra();
	
	out << d[n][mask];
}