Cod sursa(job #2964422)

Utilizator Luka77Anastase Luca George Luka77 Data 12 ianuarie 2023 22:50:47
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
#define FOR(WHATEVER) for(int i = 1; i <= WHATEVER; ++ i)
using namespace std;

/// INPUT / OUTPUT
const string problem = "sate";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");

/// GLOBAL VARIABLES
const int NMAX = 3e4+5, MOD = 1e9 + 7;
int N, M, X, Y;
bool viz[NMAX];
int dist[NMAX];
vector<pair<int,int>>adj[NMAX];

inline void bfs(int node)
{
    queue<pair<int,int>>q;
    q.push({node, 0});
    viz[node] = 1;
    while(!q.empty())
    {
        pair<int,int> curr_node = q.front();
        q.pop();
        for(auto new_node : adj[curr_node.first])
        {
            if(!viz[new_node.first])
            {
                if(curr_node.first < new_node.first)
                    dist[new_node.first] = dist[curr_node.first] + new_node.second;
                else
                    dist[new_node.first] = dist[curr_node.first] - new_node.second;
                viz[new_node.first] = 1;
                q.push({new_node.first, new_node.second});
            }
        }
    }
}

/// SOLUTION
inline void solution()
{
    bfs(X);
    fout << dist[Y];
}

/// READING THE INPUT
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> N >> M >> X >> Y;

    FOR(M)
    {
        int node1, node2, d;
        fin >> node1 >> node2 >> d;
        adj[node1].push_back({node2, d});
        adj[node2].push_back({node1, d});
    }

    solution();
}