Cod sursa(job #3174362)

Utilizator alexandru_ioan.06Alexandru Ioan alexandru_ioan.06 Data 24 noiembrie 2023 18:04:30
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <bits/stdc++.h>

using namespace std;

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

const int maxDim = 3e4 + 5;

int N , M , X , Y , ans;
bool viz[maxDim];
vector <pair <int , int>> adList[maxDim];

void BFS (int n)
{
    queue <pair<int , int>> Q;
    Q.push(make_pair(n , 0));
    viz[n] = 1;
    while(!Q.empty())
    {
        int t = Q.front().first;
        int d = Q.front().second;
        Q.pop();
        if(t == Y)
        {
            ans = d;
            return;
        }
        for(int i = 0 ; i < adList[t].size() ; ++i)
            if(viz[adList[t][i].first] == 0)
            {
                viz[adList[t][i].first] = 1;
                Q.push(make_pair(adList[t][i].first , d + adList[t][i].second));
            }
    }
}

int main()
{
    fin >> N >> M >> X >> Y;
    for(int i = 1 ; i <= M ; ++i)
    {
        int a , b , d;
        fin >> a >> b >> d;
        adList[a].push_back(make_pair(b , d));
        adList[b].push_back(make_pair(a , -d));
    }
    BFS(X);
    fout << ans;
}