Cod sursa(job #2166005)

Utilizator LittleWhoFeraru Mihail LittleWho Data 13 martie 2018 15:01:00
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.38 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 2000;
int n, m, friends_count;
int friends[nmax];
int roy[nmax][nmax];
struct edge_t {
    int node, cost;
};

vector<edge_t> graph[nmax];
int dist[nmax];
struct cmp {
    bool operator() (const int& a, const int& b) const {
        return dist[a] < dist[b];
    }
};
priority_queue<int, vector<int>, cmp> pq;
bitset<nmax> visited;

void dijkstra() {
    for (int i = 2; i <= n; i++) dist[i] = 1 << 30;
    pq.push(1);

    while (!pq.empty()) {
        int node = pq.top();
        pq.pop();

        if (visited[node]) continue;
        visited[node] = true;

        for (auto &edge: graph[node]) {
            if (dist[edge.node] > dist[node] + edge.cost) {
                dist[edge.node] = dist[node] + edge.cost;
                pq.push(edge.node);
            }
        }
    }
}

int main() {
    freopen("carici.in", "r", stdin);

    freopen("ubuntzei.in", "r", stdin);
    freopen("ubuntzei.out", "w", stdout);

    scanf("%d%d%d", &n, &m, &friends_count);
    for (int i = 0; i < friends_count; i++) {
        scanf("%d", &friends[i]);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            roy[i][j] = 1 << 30;
        }
        roy[i][i] = 0;
    }

    for (int i = 0, x, y, z; i < m; i++) {
        scanf("%d%d%d", &x, &y, &z);
        roy[x][y] = z;
        roy[y][x] = z;
        //graph[x].push_back({y, z});
        //graph[y].push_back({x, z});

    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (roy[i][j] && roy[i][k] && roy[k][j]) {
                    if (roy[i][j] > roy[i][k] + roy[k][j]) {
                        roy[i][j] = roy[i][k] + roy[k][j];
                    }
                }
            }
        }
    }

    for (int i = 0; i < friends_count; i++) {
        graph[friends[i]].push_back({1, roy[1][friends[i]]});
        graph[1].push_back({friends[i], roy[1][friends[i]]});

        graph[friends[i]].push_back({n, roy[n][friends[i]]});
        graph[n].push_back({friends[i], roy[n][friends[i]]});

        for (int j = i + 1; j < friends_count; j++) {
            graph[friends[i]].push_back({friends[j], roy[friends[i]][friends[j]]});
            graph[friends[j]].push_back({friends[i], roy[friends[i]][friends[j]]});
        }
    }

    dijkstra();
    cout << dist[n] << "\n";

    return 0;
}