Pagini recente » Cod sursa (job #13544) | Cod sursa (job #2274263) | Cod sursa (job #1576846) | Cod sursa (job #2882956) | Cod sursa (job #701640)
Cod sursa(job #701640)
/*
* ubuntzei.cpp
*
* Created on: Mar 1, 2012
* Author: Tibi
*/
#define INF 0x7fffffff
#define DEBUG 0
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
#if DEBUG == 1
#warning Debug is on
#include <iostream>
#endif
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();
sort(c + 2, c + k);
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);
#if DEBUG == 1
cout<<"PERM min="<<costmin<<": ";
for (int i = 1; i <= k; i++) cout<<c[i]<<" ";
cout<<endl;
#endif
} while (next_permutation(c + 2, c + k) && k > 2);
out<<costmin<<"\n";
#if DEBUG == 1
cout<<costmin;
#endif
in.close();
out.close();
return 0;
}