Cod sursa(job #701719)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 1 martie 2012 17:28:27
Problema Ubuntzei Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
/*
 * ubuntzei.cpp
 *
 *  Created on: Mar 1, 2012
 *      Author: Tibi
 */

#define INF 0x7fffffff
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;

const int maxn = 2010;
const int maxk = 20;
int n, m, k;

int graf [maxn][maxn];
int c[maxk];

void roy()
{
	for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if(graf[i][k] && graf[k][j] && (graf[i][j] > graf[i][k] + graf[k][j] || !graf[i][j]) && i != j)
					graf[i][j] = graf[i][k] + graf[k][j];
}


int costmin = INF;
int vis[maxk];
void backtrack (int cost, int par_i)
{
	bool done = true;
	if (cost > costmin) return;

	for (int i = 2; i < k; i++)
		if (!vis[i]) {
			done = false;
			vis[i] = true;
			backtrack(cost + graf[c[par_i]][c[i]], i);
			vis[i] = false;
		}

	if (done) costmin = min(cost + graf[c[par_i]][c[k]], costmin);
}

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][y] = graf[y][x] = c;
	}

	roy();
	sort(c + 2, c + k);

	backtrack(0, 1);

	/*int costmin = INF;
	do {
		int cost = 0;

		for (int i = 1; i < k; i++)
			cost += graf[c[i]][c[i+1]];
		costmin = min(costmin, cost);

	} while (next_permutation(c + 2, c + k) && k > 2);

	out<<costmin<<"\n";*/
	out<<costmin<<"\n";

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