Cod sursa(job #2395569)

Utilizator BungerNadejde George Bunger Data 2 aprilie 2019 18:12:56
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX=3*1e4+5;
int n,m,x,y,dist[NMAX];
vector< pair<int,int> > G[NMAX];
queue <int> q;
void citire()
{
    scanf("%d%d%d%d",&n,&m,&x,&y);
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back({b,c});
        G[b].push_back({a,-c});
    }
}
void bfs()
{
    q.push(x);
    dist[x]=1;
    if(x==y) return ;
    while(!q.empty())
    {
        int nods=q.front();
        q.pop();
        for(int i=0;i<G[nods].size();i++)
        {
             if(!dist[G[nods][i].first])
            {
                dist[G[nods][i].first]=dist[nods]+G[nods][i].second;
                q.push(G[nods][i].first);
             if(G[nods][i].first==y) break;

            }
        }
    }
}
int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    citire();
    bfs();
    printf("%d",dist[y]-1);
    return 0;
}