Cod sursa(job #2717182)

Utilizator i.uniodCaramida Iustina-Andreea i.uniod Data 6 martie 2021 16:14:32
Problema Sate Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 30000+5
#define oo 2e9
using namespace std;

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

struct NodeStruct {
    int node;
    long long cost;

    bool operator < (const NodeStruct other) const {
        return cost > other.cost;
    }
};

int n, m, x, y, a, b, c;
long long visited[NMAX];
vector <NodeStruct> G[NMAX];
priority_queue <NodeStruct> pq;

long long BFS(int start, int stop)
{
    for(int i = 1; i <= n; ++ i)
        visited[i] = oo;

    pq.push({start, 0});

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

        if(visited[cnode] == oo)
            visited[cnode] = ccost;

        for(auto it : G[cnode]) {
            if(visited[it.node] == oo)
                if(it.node > cnode)
                    pq.push({it.node, ccost + it.cost});
                else
                    pq.push({it.node, ccost - it.cost});

        }
    }
    return visited[stop];
}

int main()
{
    fin >> n >> m >> x >> y;
    for(int i = 1; i <= m; i++) {
        fin >> a >> b >> c;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    BFS(x, y);
    fout << visited[y] << '\n';
    return 0;
}