Cod sursa(job #3290385)

Utilizator ireallydontcareidontcare ireallydontcare Data 30 martie 2025 16:30:36
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>

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

const int NMAX = 3e4 + 5;

vector<pair<int,int> > G[NMAX];
int cost[NMAX];

void bfs(int nod){
    queue<int> q;
    q.push(nod);
    cost[nod] = 0;
    while(!q.empty()){
        int nod_curent = q.front();
        q.pop();
        for (int i = 0; i < G[nod_curent].size(); i++){
            int nod_nou = G[nod_curent][i].first;
            if(cost[nod_nou] == 0){
                cost[nod_nou] = cost[nod_curent] + G[nod_curent][i].second;
                q.push(nod_nou);
            }
        }
    }
}

int main(){
    int N, M, start, final;
    fin >> N >> M >> start >> final;
    while(M--){
        int x, y, d;
        fin >> x >> y >> d;
        G[x].push_back(make_pair(y, d));
        G[y].push_back(make_pair(x, -d));
    }
    bfs(start);
    fout << cost[final];
}