Cod sursa(job #701580)

Utilizator chibicitiberiuChibici Tiberiu chibicitiberiu Data 1 martie 2012 16:38:33
Problema Ubuntzei Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
/*
 * ubuntzei.cpp
 *
 *  Created on: Mar 1, 2012
 *      Author: Tibi
 */

#define INF 0x7fffffff
#define DEBUG 0

#if DEBUG == 1
#include <iostream>
#warning Debug is on
#endif

#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 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();

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

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

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

	out<<costmin;
#if DEBUG == 1
	cout<<costmin;
#endif

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