Cod sursa(job #3148736)

Utilizator VictorE07Economu Victor VictorE07 Data 3 septembrie 2023 21:47:33
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.38 kb
// DIJKSTRA's Algorithm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
//ifstream f("dijkstra.in.txt");
//ofstream g("dijkstra.out.txt");
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");

const unsigned MAX_LIMIT = 2147483647;
struct rule
{
    bool operator()(pair<unsigned, unsigned> a, pair<unsigned, unsigned> b)
    {
        return a.second > b.second;
    }
};

void Print_Graph(vector<pair<unsigned, unsigned>> *graph, unsigned n)
{
    for (int i = 1; i <= n; i++)
    {
        cout << i << " :  ";
        const unsigned num = graph[i].size();
        for (int j = 0; j < num; j++)
        {
            cout << graph[i][j].first << ", " << graph[i][j].second << "   ";
        }
        cout << "\n";
    }
    cout << "\n\n";
}

void Dijkstra(vector<pair<unsigned, unsigned>>* graph, unsigned n, unsigned start)
{
    unsigned* distance;
    bool *visited;
    distance = new unsigned[n + 1];
    visited = new bool[n + 1];
    unsigned *answers;
    answers = new unsigned[n + 1];
    for (int i = 0; i <= n; i++)
    {
        distance[i] = MAX_LIMIT;
        visited[i] = false;
        answers[i] = 0;
    }
    priority_queue<pair<unsigned, unsigned>, vector<pair<unsigned, unsigned>>, rule> pq;
    distance[start] = 0;
    pq.push(make_pair(start, distance[start]));
    while (!pq.empty())
    {
        unsigned node = pq.top().first;
        unsigned dst = pq.top().second;
        visited[node] = true;
        answers[node] = dst;
        pq.pop();
        const unsigned sz = graph[node].size();
        for (int i = 0; i < sz; i++)
        {
            if (!visited[graph[node][i].first] && distance[node] + graph[node][i].second < distance[graph[node][i].first])
            {
                distance[graph[node][i].first] = distance[node] + graph[node][i].second;
                pq.push(make_pair(graph[node][i].first, distance[graph[node][i].first]));
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (i == start)
            continue;
        g << answers[i] << " ";
    }
    delete[] distance;
    delete[] visited;
    delete[] answers;
}
int main()
{
    unsigned n, m; // 'n' nodes, 'm' edges
    unsigned a, b, length;
    vector<pair<unsigned, unsigned>> *graph;
    f >> n >> m;
    graph = new vector<pair<unsigned, unsigned>>[n + 1];
    for (int i = 0; i < m; i++)
    {
        f >> a >> b >> length;
        graph[a].push_back(make_pair(b, length));
    }
    f.close();
    // Print_Graph(graph, n);
    Dijkstra(graph, n, 1);
    ///
    delete[] graph;
    g.close();
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file