Cod sursa(job #1895921)

Utilizator tonisnakesBoar Antonio tonisnakes Data 28 februarie 2017 12:07:57
Problema Ubuntzei Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>
#define NMAX 2005
#define MMAX 10005
#define CMAX 32770
#define inf 0x3f3f3f3f
using namespace std;

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

int n, m, k, dist[NMAX][CMAX];
short ubuntzel[16];
bitset <CMAX> checked[NMAX];
vector <pair <int, int> > muchii[NMAX];
struct Drum {
	int nod, dist, cod;	
	Drum (int x, int y, int z) {
		nod = x;
		dist = y;
		cod = z;
	}
	bool operator<(const Drum& altu) const {
		return dist > altu.dist;
	}
};
priority_queue <Drum> pq;

int dijkstra () {
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < (1 << k); ++j) {
			dist[i][j] = inf;
		}
	}
	dist[1][0] = 0;
	pq.push(Drum(1,0,0));
	int mindist = inf;
	while (pq.size()) {
		Drum d = pq.top();
		pq.pop();
		if (checked[d.nod][d.cod]) {
			continue;
		}
		checked[d.nod][d.cod] = 1;
		for (int i = 0; i < muchii[d.nod].size(); ++i) {
			Drum nou = Drum(muchii[d.nod][i].first, dist[d.nod][d.cod] + muchii[d.nod][i].second, d.cod);
			for (int j = 1; j <= k; ++j) {
				if (ubuntzel[j] == nou.nod) {
					nou.cod |= (1 << (j - 1));
				}
			}
			if (nou.dist < dist[nou.nod][nou.cod]) {
				dist[nou.nod][nou.cod] = nou.dist;
				if (!checked[nou.nod][nou.cod]) {
					pq.push(nou);
				}
			}
		}
	}
	return dist[n][(1 << k) - 1];
}

int main ()
{
	fin >> n >> m;
	fin >> k;
	int x, y, c;
	for (int i = 1; i <= k; ++i) {
		fin >> x;
		ubuntzel[i] = x;
	}
	for (int i = 1; i <= m; ++i) {
		fin >> x >> y >> c;
		muchii[x].push_back(make_pair(y,c));
		muchii[y].push_back(make_pair(x,c));
	}
	fout << dijkstra();
	/*for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < (1 << k); ++j) {
			cout << dist[i][j] << " ";
		}
		cout << endl;
	}*/
	
	fin.close();
	fout.close();
	return 0;
}