Cod sursa(job #2465381)

Utilizator aurelionutAurel Popa aurelionut Data 30 septembrie 2019 01:21:14
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <queue>
#include <cstdio>
#include <algorithm>

using namespace std;

const int NMAX = 30005;
int nodes, edges, X, Y;
vector < pair <int, int> > graph[NMAX];
int dist[NMAX];
queue <int> q;

int main()
{
    FILE *fin = fopen("sate.in", "r");
    FILE *fout = fopen("sate.out", "w");
    fscanf(fin, "%d%d%d%d", &nodes, &edges, &X, &Y);
    for (int i = 1;i <= edges;++i)
    {
        int x, y, d;
        fscanf(fin, "%d%d%d", &x, &y, &d);
        graph[x].push_back(make_pair(y, d));
        graph[y].push_back(make_pair(x, d));
    }
    q.push(X);
    while (!q.empty())
    {
        int node = q.front();
        if (node == Y)
            break;
        q.pop();
        for (auto &next : graph[node])
        {
            if (dist[next.first] == 0)
            {
                if (next.first > node)
                    dist[next.first] = dist[node] + next.second;
                else
                    dist[next.first] = dist[node] - next.second;
                q.push(next.first);
            }
        }
    }
    fprintf(fout, "%d\n", dist[Y]);
    return 0;
}