Cod sursa(job #3305884)

Utilizator Tudor_11Tudor Ioan Calin Tudor_11 Data 5 august 2025 21:39:43
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector<pair<int,int>> mat[30001];
int dist[30001];
void bfs(int x)
{
    queue<int> Q;
    dist[x]=1;
    Q.push(x);
    while(!Q.empty())
    {
        int i=Q.front();
        Q.pop();
        for(auto it:mat[i])
        {
            if(dist[it.first]==0)
            {
                dist[it.first]=dist[i]+it.second;
                Q.push(it.first);
            }
        }
    }
}
int main()
{
    int n,m,x,y,a,b,d;
    fin>>n>>m>>x>>y;
    for(int i=0;i<m;i++)
    {
        fin>>a>>b>>d;
        if(a>b) swap(a,b);
        mat[a].push_back({b,d});
        mat[b].push_back({a,-d});
    }
    bfs(x);
    fout<<dist[y]-1;
    return 0;
}