Cod sursa(job #3211448)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 9 martie 2024 12:42:05
Problema Ubuntzei Scor 20
Compilator cpp-64 Status done
Runda 9_martie_simulare_oji_2024_clasele_11_12 Marime 1.75 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAX_SIZE = 10000;
const int MAX_COST = 100000;

vector<int> graph[MAX_SIZE + 1];

map<pair<int, int>, int> cost;

long long minCost[MAX_SIZE];

bool isVisited[MAX_SIZE + 1];

void dijkstra() {
    priority_queue<pair<long long, long long>> q;
    q.push({0, 1});
    minCost[1] = 0;
    while (q.empty() == 0) {
        pair<long long, long long> currentNode = q.top();
        q.pop();
        isVisited[currentNode.second] = 1;
        if (-currentNode.first > minCost[currentNode.second]) {
            continue;
        }
        for (vector<int>::iterator it = graph[currentNode.second].begin(); it < graph[currentNode.second].end(); ++it) {
            if (isVisited[*it] == 0 && -currentNode.first + cost[{currentNode.second, *it}] < minCost[*it]) {
                minCost[*it] = -currentNode.first + cost[{currentNode.second, *it}];
                q.push({-minCost[*it], *it});
            }
        }
    }
}

void solve() {
    int noNodes, noEdges;
    fin >> noNodes >> noEdges;
    int k;
    fin >> k;
    for (int i = 1; i <= k; ++i) {
        int number;
        fin >> number;
    }
    for (int i = 1; i <= noEdges; ++i) {
        int startNode, endNode, value;
        fin >> startNode >> endNode >> value;
        cost[{startNode, endNode}] = value;
        cost[{endNode, startNode}] = value;
        graph[startNode].push_back(endNode);
        graph[endNode].push_back(startNode);
    }
    for (int i = 1; i <= noNodes; ++i) {
        minCost[i] = (long long)MAX_SIZE * MAX_COST + 1;
    }
    dijkstra();
    fout << minCost[noNodes];
}

int main() {
    solve();
    return 0;
}