Cod sursa(job #3252169)

Utilizator TonyyAntonie Danoiu Tonyy Data 28 octombrie 2024 19:10:00
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int Max = 3e4 + 1;
const int Inf = 1 << 30;

vector<pair<int,int>> graph[Max];
vector<int> d(Max, Inf);

void bfs(int node)
{
    queue<pair<int,int>> q;
    q.push({0, node});
    d[node] = 0;
    while(!q.empty())
    {
        node = q.front().second;
        q.pop();
        for(auto i: graph[node])
        {
            int neighbour = i.first;
            int cost = i.second;
            if(d[node] + cost < d[neighbour])
            {
                d[neighbour] = d[node] + cost;
                q.push({d[neighbour], neighbour});
            }
        }
    }
}

int main()
{
    int n, m, s, f, x, y, z;
    fin >> n >> m >> s >> f;
    for(int i = 1; i <= m; ++i)
    {
        fin >> x >> y >> z;
        graph[x].push_back({y, z});
        graph[y].push_back({x, -z});
    }
    bfs(s);
    fout << d[f];

    fin.close();
    fout.close();
    return 0;
}