Cod sursa(job #3237103)

Utilizator maryyMaria Ciutea maryy Data 4 iulie 2024 20:40:54
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int nmax = 3e4;
int dist[nmax+1];
vector <pair<int, int>> graph[nmax+1];
int n, m, x, y;
void bfs()
{
    queue <int> q;
    q.push(x);
    dist[x] = 1;
    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        for(auto it:graph[nod])
        {
            if(dist[it.first]==0)
            {
                if(it.first<nod)//o ia in spate
                {
                    dist[it.first]=dist[nod]-it.second;
                    q.push(it.first);
                }
                else
                {
                    dist[it.first]=dist[nod]+it.second;
                    q.push(it.first);
                }
            }
        }
    }
}
int main()
{
    int a, b, d;
    in>>n>>m>>x>>y;
    for(int i=1; i<=m; i++)
    {
        in>>a>>b>>d;
        graph[a].push_back({b, d});
        graph[b].push_back({a, d});
    }
    bfs();
    out<<dist[y]-1;
}