Cod sursa(job #3207477)

Utilizator Mihai_PopescuMihai Popescu Mihai_Popescu Data 26 februarie 2024 11:23:26
Problema Ubuntzei Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

#define NMAX 205
#define INF 1000000000

vector<pair<int, int>> G[NMAX];
int localitate[NMAX];
int dist[NMAX][(1 << 10) + 5];
priority_queue<pair<int, pair<int, int>>> PQ;

void dijkstra(int n, int nr) {
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < (1 << 10); ++j) {
            dist[i][j] = INF;
        }
    }

    dist[1][nr] = 0;
    PQ.push({0, {1, nr}});
    while (PQ.size()) {
        auto aux = PQ.top();
        PQ.pop();

        int nod = aux.second.first;
        int conf = aux.second.second;

        for (auto it : G[nod]) {
            if (dist[nod][conf] + it.second < dist[it.first][conf]) {
                dist[it.first][conf] = dist[nod][conf] + it.second;
                PQ.push({-dist[it.first][conf], {it.first, conf}});
            }
            if (localitate[it.first] && (conf & (1 << localitate[it.first]))) {
                int new_conf = conf - (1 << localitate[it.first]);
                if (dist[nod][conf] + it.second < dist[it.first][new_conf]) {
                    dist[it.first][new_conf] = dist[nod][conf] + it.second;
                    PQ.push({-dist[it.first][new_conf], {it.first, new_conf}});
                }
            }
        }
    }
}

int main() {
    int n, m;
    fin >> n >> m;
    int k;
    fin >> k;
    int nr = 0;
    for (int i = 1; i <= k; ++i) {
        int oras;
        fin >> oras;
        localitate[oras] = i;
        nr += (1 << i);
    }
    while (m--) {
        int x, y, z;
        fin >> x >> y >> z;
        G[x].push_back({y, z});
        G[y].push_back({x, z});
    }

    dijkstra(n, nr);

    fout << dist[n][0];
    return 0;
}