Cod sursa(job #1003134)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 29 septembrie 2013 20:00:43
Problema Team Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 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];
bool m[MAX_N];

inline void compute(int st, int t) {
    memset(Dist[t], INF, sizeof(Dist[t]));
    memset(m, 0, sizeof(m));

    Dist[t][st] = 0;
    for(int i = 1; i <= N; ++i) {
        int x = 0;
        for(int j = 1; j <= N; ++j)
            if(!m[j] && Dist[t][j] < Dist[t][x])
                x = j;
        m[x] = 1;
        for(size_t j = 0; j < v[x].size(); ++j) {
            int y = v[x][j].first, c = v[x][j].second;
            if(Dist[t][x] + c < Dist[t][y])
                Dist[t][y] = Dist[t][x] + c;
        }
    }
}

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)
        compute(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;
}