Cod sursa(job #2275415)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 3 noiembrie 2018 10:45:23
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
///           x     h
vector <pair<int , int> > h[300001];
queue<int> q;
int d[30001] , X , Y , n , m;
///d[i] = distanta de la X la i
bitset<300001>viz;
void Dfs(int nod)
{
    viz[nod]=1;
    for(auto i:h[nod])
        if(!viz[i.first])Dfs(i.first);

    cout<<nod <<"  "<<h[nod][1].second <<"\n";
}

int main()
{
    int i , x , y ,dist , sum , j;
    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});
    }
    ///Dfs(X);
    for(i = 1; i <= n; i++)
        d[i] = -1;
    d[X] = 0;
    q.push(X);
    sum = 0;
    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;
}