Cod sursa(job #3312704)

Utilizator Tudor28Ceclan Tudor Tudor28 Data 29 septembrie 2025 16:30:43
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
#define NMAX 350005
vector<pair<int, int>> graph[NMAX];
int viz[NMAX];
int cost[NMAX];
int solve(int start, int target)
{
    queue<int> q;
    q.push(start);
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        viz[x] = 1;
        if (x == target)
        {
            return cost[x];
        }
        for (auto i : graph[x])
        {
            if (viz[i.first])
                continue;

            cost[i.first] = cost[x] + i.second;
            q.push(i.first);
        }
    }
    return -1;
}
int main()
{
    int n, m, x, y;
    fin >> n >> m >> x >> y;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        fin >> a >> b >> c;
        graph[a].push_back({b, c});
        graph[b].push_back({a, -c});
    }
    fout << solve(x, y);
    return 0;
}