Cod sursa(job #1082071)

Utilizator Johny_Depp22Johnny Depp Johny_Depp22 Data 14 ianuarie 2014 09:58:39
Problema Ubuntzei Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");

int N, M, K, p[15], a[205][205], x, y, z, sol, cost;

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

    sort(p+1, p+K+1);

    for (int i=1; i<=N; ++i)
        for (int j=1; j<=N; ++j)
            if (i!=j) a[i][j]=200000000;

    for (int i=1; i<=M; ++i)
        f>>x>>y>>z, a[x][y]=a[y][x]=z;

    for (int k=1; k<=N; ++k)
        for (int i=1; i<=N; ++i)
            for (int j=1; j<=N; ++j)
                if (i!=j && a[i][j]>a[i][k]+a[j][k])
                    a[i][j]=a[i][k]+a[k][j];

    //if (K==0) { g<<a[1][N]<<'\n'; return 0; }

    sol=a[1][p[1]];
    for (int i=1; i<K; ++i)
        sol+=a[p[i]][p[i+1]];
    sol+=a[p[K]][N];

    while (next_permutation(p+1, p+K+1))
    {
        cost=a[1][p[1]];
        for (int i=1; i<K; ++i)
            cost+=a[p[i]][p[i+1]];
        cost+=a[p[K]][N]; sol=min(sol, cost);
    }

    g<<sol<<'\n';
    return 0;
}