Cod sursa(job #3343426)

Utilizator McMeatGhenea Radu Stefan McMeat Data 27 februarie 2026 13:25:20
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <algorithm>
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
#define STDIN 0
#if STDIN
    #define fein cin
    #define fout cout
#else
    ifstream fein("sate.in");
    ofstream fout("sate.out");
#endif

const int NMAX=30001;
const int PRIMEMAX=1e9+1;
const int INF = 1e9+1;
const int MOD = 1e9+9;

int n, m, x, y;
struct edge {
    int node, cost;
};
vector<edge> l[NMAX];
int bfs(int start, int end) {
    deque<int> q;
    vector<int> pos(NMAX, 0);
    q.push_back(start);
    while(!q.empty()) {
        int nc=q.front();
        if(nc==end) return pos[nc];
        q.pop_front();
        for(edge e : l[nc]) {
            int vc=e.node;
            if(!pos[vc]) {
                pos[vc]=pos[nc]+e.cost;
                q.push_back(vc);
            }
        }
    }
    return -1;
}
void read_data() {
    fein>>n>>m>>x>>y;
    for(int i=1;i<=m;i++) {
        int a, b, c; fein>>a>>b>>c;
        if(a>b) swap(a,b);
        l[a].push_back({b, c});
        l[b].push_back({a, -c});
    }
}
int main()
{
    read_data();
    fout<<bfs(x, y);
    return 0;
}