Cod sursa(job #1003125)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 29 septembrie 2013 19:52:41
Problema Team Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <vector>
#include <queue>

#include <cstdio>
#include <cstring>
using namespace std;

const int MAX_N = 502;
const int MAX_P = 52;
const int INF = 0x3f3f3f3f;

int P, N, M;
int D[MAX_P][MAX_P][MAX_P], Dist[MAX_P][MAX_N], a[MAX_P];
queue < int > Q;
vector < pair < int, int > > v[MAX_N];

inline void BF(int st, int t) {
    memset(Dist[t], INF, sizeof(Dist[t]));
    Dist[t][st] = 0;
    Q.push(st);
    while(!Q.empty()) {
        int x = Q.front();
        Q.pop();
        for(size_t i = 0; i < v[x].size(); ++i) {
            int y = v[x][i].first, c = v[x][i].second;
            if(Dist[t][x] + c < Dist[t][y]) {
                Dist[t][y] = Dist[t][x] + c;
                Q.push(y);
            }
        }
    }
}

int main() {
    freopen("team.in", "r", stdin);
    freopen("team.out", "w", stdout);

    scanf("%d %d %d", &P, &N, &M);
    for(int i = 1, x, y, c; i <= M; ++i) {
        scanf("%d %d %d", &x, &y, &c);
        v[x].push_back(make_pair(y, c));
        v[y].push_back(make_pair(x, c));
    }
    for(int i = 1; i <= P; ++i)
        scanf("%d", &a[i]);
    a[0] = 1;

    for(int i = 0; i <= P; ++i)
        BF(a[i], i);

    for(int q = 0; q < P; ++q)
        for(int i = 1; i <= P; ++i) {
            int j = i + q;
            if(j > P)
                continue;
            for(int k = 0; k <= P; ++k) {
                D[i][j][k] = INF;
                for(int h = i; h <= j; ++h)
                    D[i][j][k] = min(D[i][j][k], D[i][h-1][h] + D[h+1][j][h] + Dist[k][a[h]]);
            }
        }

    printf("%d\n", D[1][P][0]);

    fclose(stdin);
    fclose(stdout);

    return 0;
}