Cod sursa(job #701529)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 1 martie 2012 16:22:16
Problema Ubuntzei Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
/*
 * ubuntzei.cpp
 *
 *  Created on: Mar 1, 2012
 *      Author: Tibi
 */
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;

#define INF 0x7fffffff

const int maxn = 2010;
const int maxk = 20;
typedef pair<int, int> edge;

vector<int> graf [maxn];
vector<int> cost [maxn];
int n, m, k;

int c[maxk], d[maxn];
int vis[maxn];

int dd[maxk][maxk];

void dijkstra(int src)
{
	// Initialize distances
	for (int i = 1; i<=n; i++) d[i] = INF;
	memset(vis, 0, sizeof(int) * (n + 1));

	priority_queue<edge> q;
	q.push(make_pair(0, src));
	d[src] = 0;

	while (!q.empty())
	{
		int c = q.top().first,
			x = q.top().second;
		q.pop();

		if (vis[x]) continue;
		vis[x] = 1;

		for (unsigned i = 0; i < graf[x].size(); i++)
			if (d[graf[x][i]] > c + cost[x][i])
			{
				d[graf[x][i]] = c + cost[x][i];
				q.push(make_pair(d[graf[x][i]], graf[x][i]));
			}
	}
}

int main()
{
	ifstream in ("ubuntzei.in");
	ofstream out("ubuntzei.out");

	in>>n>>m>>k;

	c[1] = 1;
	for (int i = 2; i <= k + 1; i++)
		in>>c[i];
	c[k+2] = n;
	k+=2;

	for (int i = 0; i < m; i++) {
		int x,y,c;
		in>>x>>y>>c;

		graf[x].push_back(y);
		cost[x].push_back(c);
		graf[y].push_back(x);
		cost[y].push_back(c);
	}

	for (int i = 1; i <= k; i++)
	{
		dijkstra(c[i]);
		for (int j = 1; j <= k; j++)
			dd[i][j] = d[c[j]];
	}

	int costmin = INF;
	do {
		int cst = 0;

		for (int i = 1; i < k; i++)
			cst += dd[i][i+1];

		costmin = min(costmin, cst);
	} while (next_permutation(c + 2, c + k - 1) && k > 2);

	out<<costmin;

	in.close();
	out.close();
	return 0;
}