Cod sursa(job #3289702)

Utilizator Victor5539Tanase Victor Victor5539 Data 28 martie 2025 10:48:33
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define int long long

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

const int MAX=30000;
int n,m,x,y,i,nod1,nod2,cost,sum;
bool viz[MAX+5];
vector <pair<int,int>> muchii[MAX+5];
queue <pair<int,int>> q;
signed main()
{
    fin>>n>>m>>x>>y;

    while (m)
    {
        fin>>nod1>>nod2>>cost;

        muchii[nod1].push_back({nod2,cost});
        muchii[nod2].push_back({nod1,cost});
        m--;
    }

    q.push({x,0});

    while (!q.empty())
    {
        nod1=q.front().first;
        sum=q.front().second;

        if (nod1==y)
        {
            fout<<sum;
            return 0;
        }

        for (auto x: muchii[nod1])
            if (!viz[x.first])
            {
                viz[x.first]=1;
                if (x.first>nod1)
                q.push({x.first,sum+x.second});
                else
                q.push({x.first,sum-x.second});
            }

        q.pop();
    }


    fout<<-1;


    return 0;
}