Cod sursa(job #1008338)

Utilizator mvcl3Marian Iacob mvcl3 Data 10 octombrie 2013 21:07:53
Problema Ubuntzei Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>
#define IN "ubuntzei.in"
#define OUT "ubuntzei.out"
#define MAX_SIZE 2009
#define oo 1000000

std :: ifstream f(IN);
std :: ofstream g(OUT);

int N, M, K;
int FRIEND[MAX_SIZE];
int D[MAX_SIZE][MAX_SIZE];

inline void READ_DATA()
{
    int x , y , z;

    f >> N >> M >> K;
    for(int i = 1; i <= K; ++i) f >> FRIEND[i];

    for(int i = 1; i <= M; ++i)
    {
        f >> x >> y >> z;
        D[x][y] = D[y][x] = z;
    }
}

inline void RF()
{
    for(int  k = 1; k <= N; ++k)
        for(int i = 1; i <= N; ++i)
            for(int j = 1; j <= N; ++j)
                if(D[i][k] && D[k][j] && (i != j) && (D[i][j] > D[i][k] + D[k][j] || !D[i][j]))
                    D[i][j] = D[i][k] + D[k][j];
}

void SOLVE()
{
    RF();
    int BestSol = oo;
    do
    {
        int Cost = D[1][FRIEND[1]];
        for(int i = 1; i < K; ++i) Cost += D[FRIEND[i]][FRIEND[i + 1]];
        Cost += D[FRIEND[K]][N];

        BestSol = std :: min(BestSol, Cost);
    }
    while(std :: next_permutation(FRIEND + 1, FRIEND + K + 1));

    g << BestSol << '\n';
}

int main()
{
    READ_DATA();
    SOLVE();

    g.close();
    return 0;
}