Cod sursa(job #2633992)

Utilizator CraniXortDumitrescul Eduard CraniXort Data 9 iulie 2020 15:20:02
Problema Team Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.16 kb
#include <bits/stdc++.h>
#define maxn 55
#define maxv 505

std::ifstream fin("team.in");
std::ofstream fout("team.out");

int x[maxn];
int dist[maxv][maxv];
int d[maxv][maxv];
int dest[maxn];
int dp[maxn][maxn][maxn];

void computeMinDist(int V){
    int i, j, k;
    for (k=1; k<=V; k++){
        for (i=1; i<=V; i++){
            if (i == k) continue;
            for (j=1; j<=V; j++){
                if (j == k and j == i) continue;

                dist[dest[i]][dest[j]] = std::min (dist[dest[i]][dest[j]], dist[dest[i]][dest[k]] + dist[dest[k]][dest[j]]);
            }
        }
    }
}

void dijkstra (int src, int V){
    int i, min, pos;
    char U[maxv];

    for (i=1; i<=V; i++){
        dist[src][i] = 1e8;
        U[i] = 0;
    }
    dist[src][src] = 0;

    while (true)
    {
        min = 1e8;
        pos = -1;
        for (i=1; i<=V; i++)
            if (U[i] == 0 and min > dist[src][i])
                min = dist[src][i],
                pos = i;

        if (pos == -1) break;
        U[pos] = 1;

        for (i=1; i<=V; i++)
            if (dist[src][i] > dist[src][pos]+d[pos][i])
                dist[src][i] = dist[src][pos]+d[pos][i];
    }
}

/*
void dijkstra(int src, int dist[])
{
    int i, min, pos;
    char U[MAX_N];

    FOR (i, 0, N) { dist[i] = INF; U[i] = 0; }
    dist[src] = 0;

    while (1)
    {
        min = INF; pos = -1;
        FOR (i, 0, N) if (!U[i] && min > dist[i])
            min = dist[i], pos = i;

        if (pos == -1) break;
        U[pos] = 1;

        FOR (i, 0, N) if (dist[i] > dist[pos]+G[pos][i])
            dist[i] = dist[pos]+G[pos][i];
    }
}
*/

int main()
{
    int n, V, E, src, dst, cost, i, j, k, u;
    fin >> n >> V >> E;

    for (i=1; i<=V; i++)
    for (j=1; j<=V; j++)
        dist[i][j] = 1e8,
        d[i][j] = 1e8;

    for (i=1; i<=V; i++)
        dist[i][i] = 0,
        d[i][i] = 0;

    for (i=0; i<E; i++) {
        fin >> src >> dst >> cost;
        d[src][dst] = cost;
        d[dst][src] = cost;
    }



    /*
    for (i=1; i<=V; i++, fout << '\n')
        for (j=1; j<=V; j++)
        fout << dist[i][j] << ' ';
        */

    for (i=1; i<=n; i++)
        fin >> dest[i];
    //computeMinDist(n);
    for (i=1; i<=V; i++)
        dijkstra (i, V);

    for (i=1; i<=n; i++)
        dp[i][i][i] = 0;

    for (int l=0; l<=n; l++)
    for (i=1; i<=n-l; i++){
        j = i + l;
        for (k=1; k<=n; k++){
            if (k == i and k == j)
            continue;
            dp[i][j][k] = 1e9;
            for (u=i; u<=j; u++){
                //std::cout << dp[i][u-1][u] << ' ' << dp[u+1][j][i]  << ' ' << dist[dest[u]][dest[k]] << dp[i][u-1][u] + dp[u+1][j][i] + dist[dest[u]][dest[k]] << '\n';
                //std::cout << std::min (dp[i][j][k], dp[i][u-1][u] + dp[u+1][j][u] + dist[dest[u]][dest[k]]) << '\n';
                dp[i][j][k] = std::min (dp[i][j][k], dp[i][u-1][u] + dp[u+1][j][u] + dist[dest[u]][dest[k]]);
            }
        }
    }



    int ans = 1e9;
    for (i=1; i<=n; i++)
    ans = std::min (ans, dp[1][n][i] + dist[1][dest[i]]);

    fout << ans;

    return 0;
}