Cod sursa(job #1603443)

Utilizator ArkinyStoica Alex Arkiny Data 17 februarie 2016 15:47:58
Problema Ubuntzei Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;

#define Node pair<int,int>
#define mk make_pair
#define d first
#define c second

#define  MAX 2010
#define  INFINITY (1<<30)

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

struct Comparator
{
	bool operator () (Node const &e1, Node const &e2)
	{
		return e1.c > e2.c;
	}
};

vector<Node> L[MAX];
priority_queue<Node, vector<Node>, Comparator> H;

int D[MAX],O[MAX],N,M,K;

int Dijkstra(int S, int it)
{
	H.push(mk(S, D[S]));
	while (H.size())
	{
		Node el = H.top();
		H.pop();
		if (O[el.d] == 1 && it != K + 1)
		{
			O[el.d] = 2;
			while (H.size())
				H.pop();
			return el.d;

		}
		else if (it == K + 1 && el.d == N)
			return 0;

		for (int i = 0;i < L[el.d].size();++i)
		{
			if (el.c + L[el.d][i].c < D[L[el.d][i].d])
			{
				D[L[el.d][i].d] = el.c + L[el.d][i].c;
				H.push(mk(L[el.d][i].d, D[L[el.d][i].d]));
			}
		}

	}
}

int main()
{
	in >> N >> M >> K;

	for (int i = 1;i <= K;++i)
	{
		int x;
		in >> x;
		O[x] = 1;
	}
	
	for (int i = 1;i <= M;++i)
	{
		int x, y, cost;
		in >> x >> y >> cost;
		L[x].push_back(mk(y, cost));
		L[y].push_back(mk(x, cost));
	}

	int el = 1;
	for (int i = 1;i <= K+1;++i)
	{
		for (int j = 1;j <= N;++j)
			if(j!=el)
				D[j] = INFINITY;

		el = Dijkstra(el, i);

	}

	out << D[N];
	


	return 0;
}