Cod sursa(job #3373647)

Utilizator AndreiO86902462Andrei O AndreiO86902462 Data 7 noiembrie 2026 14:21:39
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int main()
{
    int n, m, x, y; fin >> n >> m >> x >> y;
    --x; --y;
    
    vector<vector<pair<int, int>>> neighbors = {};
    neighbors.resize(n);
    
    for(int idx = 0; idx < m; ++idx) {
        int i, j, d; fin >> i >> j >> d;
        --i; --j;
        
        neighbors[i].push_back({j, d});
        neighbors[j].push_back({i, -d});
    }
    
    vector<bool> visited = {}; visited.resize(n);
    vector<int> dist = {}; dist.resize(n);
    queue<int> q = {}; q.push(x);
    
    while(!q.empty()) {
        int n = q.front(); q.pop();
        
        if(visited[n])
            continue;
            
        visited[n] = true;
        
        for(pair<int,int> neighbor_and_cost: neighbors[n]) {
            int neighbor_idx = neighbor_and_cost.first;
            int cost = neighbor_and_cost.second;
            dist[neighbor_idx] = dist[n] + cost;
            
            if(neighbor_idx == y) {
                break;
            }
            
            q.push(neighbor_idx);
        }
    }
    
    fout << dist[y];
    return 0;
}