Cod sursa(job #1154096)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 25 martie 2014 22:50:33
Problema Team Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.16 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const int oo = 0x3f3f3f3f;
const int NIL = -1;

vector< vector< pair<int, int> > > G;
int N, V, Source, Answer;
vector<int> Destinations;
vector< vector<int> > Cost;

void Dijkstra(const int source, vector<int> &distances) {
    distances = vector<int>(V, oo);
    vector<bool> used = vector<bool>(V, false);
    distances[source] = 0;
    int x = source;
    do {
        for (vector< pair<int, int> >::const_iterator e = G[x].begin(); e != G[x].end(); ++e)
            distances[e->first] = min(distances[e->first], distances[x] + e->second);
        used[x] = true;
        x = NIL;
        for (int y = 0; y < V; ++y)
            if (!used[y] && (x == NIL || distances[y] < distances[x]))
                x = y;
    } while (x != NIL);
}

void BuildCost() {
    vector<int> vertices = Destinations;
    vertices.push_back(Source);
    sort(vertices.begin(), vertices.end());
    vertices.erase(unique(vertices.begin(), vertices.end()), vertices.end());
    vector<int> newIndex = vector<int>(V, -1);
    int newV = int(vertices.size());
    for (int i = 0; i < newV; ++i)
        newIndex[vertices[i]] = i;
    for (int x = 0; x < N; ++x)
        Destinations[x] = newIndex[Destinations[x]];
    Cost = vector< vector<int> >(newV, vector<int>(newV, oo));
    for (int i = 0; i < newV; ++i) {
        vector<int> distances;
        Dijkstra(vertices[i], distances);
        for (int j = 0; j < newV; ++j)
            Cost[i][j] = distances[vertices[j]];
    }
    V = newV;
}

void Solve() {
    BuildCost();
    vector< vector< vector<int> > > dp = vector< vector< vector<int> > >(V, vector< vector<int> >(N, vector<int>(N, oo)));
    for (int length = 1; length <= N; ++length) {
        for (int x = 0; x < V; ++x) {
            for (int first = 0, last = length - 1; last < N; ++first, ++last) {
                for (int split = first; split <= last; ++split) {
                    int current = Cost[x][Destinations[split]];
                    if (split > first)
                        current += dp[Destinations[split]][first][split - 1];
                    if (split < last)
                        current += dp[Destinations[split]][split + 1][last];
                    dp[x][first][last] = min(dp[x][first][last], current);
                }
            }
        }
    }
    Answer = dp[Source][0][N - 1];
}

inline void AddEdge(const int x, const int y, const int c) {
    G[x].push_back(make_pair(y, c));
    G[y].push_back(make_pair(x, c));
}

void Read() {
    ifstream in("team.in");
    int E;
    in >> N >> V >> E;
    G = vector< vector< pair<int, int> > >(V, vector< pair<int, int> >());
    for (; E > 0; --E) {
        int x, y, c;
        in >> x >> y >> c;
        AddEdge(--x, --y, c);
    }
    Destinations = vector<int>(N);
    for (int x = 0; x < N; ++x) {
        in >> Destinations[x];
        --Destinations[x];
    }
    Source = 0;
    in.close();
}

void Print() {
    ofstream out("team.out");
    out << Answer << "\n";
    out.close();
}

int main() {
    Read();
    Solve();
    Print();
    return 0;
}