Cod sursa(job #2275420)

Utilizator AtanaseTeodorAtanase Alexandru-Teodor AtanaseTeodor Data 3 noiembrie 2018 10:47:08
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>
#define N 30005
using namespace std;

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

vector<pair <int, int> >h[N];
queue<int> q;

int d[N], X, Y, n;
/// distanta de la X la i

int main()
{
    int i, m, x, y, j, dist, w;
    fin >> n >> m >> X >> Y;
    for(i = 1; i <= m ; i++)
    {
        fin >> x >> y >> dist;
        h[x].push_back({y, dist});
        h[y].push_back({x, -dist});

    }

    for(i = 1; i<= n ; i++)
        d[i] = -1;
    d[X] = 0;
    q.push(X);
    while(!q.empty())
    {
        x = q.front();
        q.pop();

        for(auto w : h[x])
        {
            j = w.first;
            dist = w.second;
        if (d[j] == -1)
        {
            d[j] = d[x] + dist;
            q.push(j);
            if(d[Y] >= 0)
            {
                fout << d[Y];
                return 0;
            }
        }
    }
    }
    return 0;
}