Cod sursa(job #2649728)

Utilizator blackmanta45Andrei blackmanta45 Data 16 septembrie 2020 04:20:49
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 6.41 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <climits>
#define INF 1000000000000000
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

int i, n, m, T, air, k, start, ending, cost, friend_location[20], j, no_friends;
long long  dist[100010], stack[100010];
int isFriend[100010];
bool isNode[100010];
long long C[5300025][20];
long long Cost[20][20];
vector<int> nodes;
vector<pair<int, int>> bigGraph[100001];
vector<int> smallGraph[20];

bool cmd(int a, int b) {
    if (a < b)
        return 1;
    return 0;
}

void dijkstra(int start) {
    //vector<pair<int, int>> bigGraph[10001];
    long long st, dr;
    bool visited[10010] = { 0 };
    for (unsigned int i = 0; i < nodes.size(); i++) {
        dist[nodes[i]] = INF;
    }
    dist[start] = 0;
    stack[0] = start;
    int startNodeSmall = isFriend[start];
    st = dr = 0;
    while (st <= dr) {
        for (auto node : bigGraph[stack[st]]) {
            if (!visited[node.first]) {
                if (node.second + dist[stack[st]] < dist[node.first]) {
                    long long prev_value = dist[node.first];
                    dist[node.first] = node.second + dist[stack[st]];
                    int currentSmallNode = isFriend[node.first];
                    if (currentSmallNode || node.first == 0) {
                        if (prev_value == INF) {
                            smallGraph[startNodeSmall].push_back(currentSmallNode);
                            Cost[startNodeSmall][currentSmallNode] = dist[node.first];
                        }
                        else {
                            for (auto nodeToUpdate : smallGraph[startNodeSmall])
                                if (nodeToUpdate == currentSmallNode) {
                                    Cost[startNodeSmall][nodeToUpdate] = dist[node.first] < Cost[startNodeSmall][nodeToUpdate] ? dist[node.first] : Cost[startNodeSmall][nodeToUpdate];
                                    break;
                                }
                        }
                    }
                    else
                        stack[++dr] = node.first;
                }
            }
            else {
                if (node.second + dist[stack[st]] < dist[node.first]) {
                    long long prev_value = dist[node.first];
                    dist[node.first] = node.second + dist[stack[st]];
                    int currentSmallNode = isFriend[node.first];
                    if (currentSmallNode || node.first == 0) {
                        if (prev_value == INF) {
                            smallGraph[startNodeSmall].push_back(currentSmallNode);
                            Cost[startNodeSmall][currentSmallNode] = dist[node.first];
                        }
                        else {
                            for (auto nodeToUpdate : smallGraph[startNodeSmall])
                                if (nodeToUpdate == currentSmallNode) {
                                    Cost[startNodeSmall][nodeToUpdate] = dist[node.first] < Cost[startNodeSmall][nodeToUpdate] ? dist[node.first] : Cost[startNodeSmall][nodeToUpdate];
                                    break;
                                }
                        }
                    }
                }
            }
        }
        visited[stack[st]] = 1;
        st++;
    }
    //sort(smallGraph[isTreasure[start]].begin(), smallGraph[isTreasure[start]].end(), cmd);
}

void dijkstra2(int start) {
    long long st = 0, dr = 0;
    bool visited[10010] = { 0 };
    for (unsigned int i = 0; i < nodes.size(); i++) {
        dist[nodes[i]] = INF;
    }
    while (st <= dr) {
        long long current_node = stack[st];
        for (auto node : bigGraph[current_node]) {
            if (dist[node.first] > dist[current_node] + node.second) {
                dist[node.first] = dist[current_node] + node.second;
                stack[++dr] = node.first;
            }
        }
    }
}

void HamiltonianCicle() {
    int pow2 = 1 << no_friends;
    for (int i = 0; i < pow2; ++i) {
        for (int j = 0; j < no_friends; ++j) {
            C[i][j] = INF;
        }
    }
    C[1][0] = 0;
    for (int i = 0; i < pow2; i++) {
        for (int j = no_friends - 1; j >= 0; --j) {
            if (i & (1 << j)) {
                for (size_t k = 0; k < smallGraph[j].size(); ++k) {
                    if (i & (1 << smallGraph[j][k])) {
                        C[i][j] = min(C[i][j], 1LL * C[i ^ (1 << j)][smallGraph[j][k]] + Cost[smallGraph[j][k]][j]);
                    }
                    //C[i][j] = min(C[i][j], 1LL * C[i][smallGraph[j][k]] + Cost[smallGraph[j][k]][j]);
                }
            }
        }
    }
}


void HamiltonianCicle2() {
    no_friends += 2;
    long long pow2 = 1 << no_friends;

    for (int i = 0; i < pow2; ++i) {
        for (int j = 0; j < no_friends; ++j) {
            C[i][j] = INF;
        }
    }
    C[1][0] = 0;
    for (int i = 0; i < pow2; i++) {
        for (int j = 0; j < no_friends; j++) {
            if (i & (1 << j)) {
                for (int k = 0; k <= no_friends; k++) {
                    C[i][j] = min(C[i][j], C[i - (1 << j-1)][k] + Cost[k][j]);
                }
            }
        }
    }
}

int main() {
    fin >> n >> m;

    fin >> no_friends;
    for (i = 1; i <= no_friends; i++) {
        fin >> friend_location[i];
    }
    friend_location[0] = 1;
    friend_location[no_friends + 1] = n;
    for (i = 0; i < m; i++) {
        fin >> start >> ending >> cost;
        bigGraph[start].push_back(make_pair(ending, cost));
        bigGraph[ending].push_back(make_pair(start, cost));
        if (!isNode[start])
            nodes.push_back(start), isNode[start] = 1;
        if (!isNode[ending])
            nodes.push_back(ending), isNode[ending] = 1;
    }
    for (i = 0; i <= n; i++)
        for (j = 0; j <= n; j++)
            Cost[i][j] = INF;
    for (i = 0; i <= no_friends + 1; i++) {
        dijkstra(friend_location[i]);
        for (int j = 0; j <= no_friends + 1; j++) {
            Cost[i][j] = dist[friend_location[j]];
        }
    }
    HamiltonianCicle2();
    long long solution = 1000000000;
    /*for (int i = 0; i < no_friends; i++) {
        solution = min(solution, C[(1 << no_friends) - 1][i] + Cost[i][no_friends -1]);
    }*/
    fout << C[(1<<no_friends)-1][no_friends - 1];
}