Cod sursa(job #3192275)

Utilizator oanantoniaSut Oana oanantonia Data 11 ianuarie 2024 22:43:44
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, s, f, x, y, d, i, viz[30005], dist[30005];
vector<pair<int,int>> v[30005];
queue<int> q;

void bfs( int s ){
     viz[s] = 1;
     dist[s] = 0;
     q.push(s);
     while( !q.empty() ){
            int x = q.front();
            q.pop();
            for( auto el:v[x] ){
                 if ( viz[el.first] == 0 ){
                      viz[el.first] = 1;
                      dist[el.first] = dist[x] + el.second;
                      q.push(el.first);
                 }
            }
     }
}



int main()
{
    fin >> n >> m >> s >> f;
    for ( i = 1; i <= m; i++ ){
          fin >> x >> y >> d;
          v[x].push_back({y, d});
          v[y].push_back({x, -d});
    }
    bfs(s);
    fout << dist[f];
}