Cod sursa(job #2869180)

Utilizator CiuiGinjoveanu Dragos Ciui Data 11 martie 2022 13:03:25
Problema Ubuntzei Scor 10
Compilator cpp-64 Status done
Runda wellcodesimulareclasa11-12-11martie Marime 1.62 kb
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <deque>
#include <vector>
#include <set>
#include <queue>
using namespace std;

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

const int MAX_SIZE = 10005;
const int MAX_VAL = 2000000000;

vector<pair<int, int>> graph[MAX_SIZE];
int costs[MAX_SIZE];

void dijkstra(int peaks) {
    for (int i = 2; i <= peaks; ++i) {
        costs[i] = MAX_VAL;
    }
    set<pair<int, int>> positions;
    positions.insert({0, 1});
    while (!positions.empty()) {
        int currentCost = positions.begin()->first;
        int currentPeak = positions.begin()->second;
        positions.erase(positions.begin());
        if (currentCost == costs[currentPeak]) {
            for (pair<int, int> element : graph[currentPeak]) {
                int nextPeak = element.second;
                int nextCost = element.first;
                if (costs[nextPeak] > currentCost + nextCost) {
                    costs[nextPeak] = currentCost + nextCost;
                    positions.insert({costs[nextPeak], nextPeak});
                }
            }
        }
    }
    fout << costs[peaks];
}

int main() {
    int peaks, arches;
    int k;
    fin >> peaks >> arches >> k;
    for (int i = 1; i <= k; ++i) {
        int city;
        fin >> city;
    }
    for (int i = 1; i <= arches; ++i) {
        int start, end, cost;
        fin >> start >> end >> cost;
        graph[start].push_back(make_pair(cost, end));
    }
    dijkstra(peaks);
}

/*
 
 4 5 0
 1 2 1
 1 3 1
 2 3 1
 2 4 4
 3 4 2
 
 
 
 
 */