Cod sursa(job #3257678)

Utilizator v4nes5aBulacu Gabriela-Vanessa v4nes5a Data 18 noiembrie 2024 21:00:59
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <bits/stdc++.h>
using namespace std;

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

struct muchie
{
    int v;
    long long d;
};

const int NMAX = 30000;
int n, m, x, y, i, viz[NMAX + 5];
vector<muchie> g[NMAX + 5];

void bfs()
{
    queue<pair<int, long long>> q;
    q.push({x, 0});
    viz[x] = 1;
    
    while(!q.empty())
    {
        int current = q.front().first;
        long long dist = q.front().second;
        q.pop();
        
        if(current == y)
        {
            fout << dist;
            return;
        }
        
        for(i = 0; i < (int)g[current].size(); ++i)
        {
            if(!viz[g[current][i].v])
            {
                viz[g[current][i].v] = 1;
                q.push({g[current][i].v, dist + g[current][i].d});
            }
        }
    }
    
}

int main()
{
    fin >> n >> m >> x >> y;
    for(i = 1; i <= m; ++i)
    {
        int a, b;
        long long dist;
        fin >> a >> b >> dist;
        if(a > b)
            swap(a, b);
        g[a].push_back({b, dist});
        g[b].push_back({a, -dist});
    }
    
    bfs();
}