Cod sursa(job #3237756)

Utilizator GabrielPopescu21Silitra Gabriel - Ilie GabrielPopescu21 Data 12 iulie 2024 17:34:42
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;

const int MAX = 30005;
vector<pair<int,int>> graph[MAX];
vector<int> dist;

void bfs(int start)
{
    queue<int> q;
    q.push(start);
    dist[start] = 1;

    while (!q.empty())
    {
        int node = q.front();
        q.pop();
        for (pair<int,int> next : graph[node])
        {
            if (!dist[next.first])
            {
                dist[next.first] = dist[node] + next.second;
                q.push(next.first);
            }
        }
    }
}

int main()
{
    ifstream cin("sate.in");
    ofstream cout("sate.out");
    int n, m, x, y;
    cin >> n >> m >> x >> y;
    dist.assign(n+1, 0);

    for (int i = 1; i <= m; ++i)
    {
        int x, y, cost;
        cin >> x >> y >> cost;
        graph[x].push_back({y, cost});
        graph[y].push_back({x, -cost});
    }

    bfs(x);
    cout << dist[y]-1;

    return 0;
}