Cod sursa(job #1725573)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 5 iulie 2016 22:24:28
Problema Team Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <fstream>

using namespace std;

constexpr int MAX_N = 500;
constexpr int MAX_P = 50;
constexpr int INF = 0x3f3f3f3f;

int cost[MAX_N][MAX_N];
int residence[MAX_P];
int dp[MAX_P][MAX_P][MAX_N];

int main() {
    ifstream fin("team.in");
    ofstream fout("team.out");
    fin.tie(0);
    ios_base::sync_with_stdio(false);
    int p, n, e; fin >> p >> n >> e;
    for (int i = 0; i < n; i += 1) {
        for (int j = 0; j < n; j += 1) {
            cost[i][j] = INF & -(i != j);
        }
    }
    for (int i = 0; i < e; i += 1) {
        int node_1, node_2, weight; fin >> node_1 >> node_2 >> weight;
        cost[node_1 - 1][node_2 - 1] = cost[node_2 - 1][node_1 - 1] = weight;
    }
    for (int i = 0; i < p; i += 1) {
        fin >> residence[i]; residence[i] -= 1;
    }
    for (int k = 0; k < n; k += 1) {
        for (int i = 0; i < n; i += 1) {
            for (int j = 0; j < n; j += 1) {
                if (cost[i][k] + cost[k][j] < cost[i][j]) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
            }
        }
    }
    for (int st = n - 2; st >= 0; st -= 1) {
        for (int dr = st + 1; dr < n; dr += 1) {
            for (int node = 0; node < n; node += 1) {
                int x = INF;
                for (int split = st; split <= dr; split += 1) {
                    const int option = dp[st][split - 1][residence[split]]
                                    + cost[node][residence[split]]
                                    + dp[split + 1][dr][residence[split]];
                    if (option < x) {
                        x = option;
                    }
                }
                dp[st][dr][node] = x;
            }
        }
    }
    fout << dp[0][n - 1][0] << '\n';
    fout.close();
    return 0;
}