Cod sursa(job #2395575)

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

const int NMAX=1<<15;
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);
    int a,b,c;
    while(m--)
    {

        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back({b,c});
        G[b].push_back({a,-c});
    }
}
void bfs()
{
    int nods,nod,cost;
    q.push(x);
    dist[x]=1;
    if(x==y) return ;
    while(!q.empty())
    {
        nods=q.front();
        q.pop();
        for(vector<pair<int,int> >::iterator it=G[nods].begin();it!=G[nods].end();it++)
        {
             nod=it->first;
             cost=it->second;
             if(!dist[nod])
            {
                dist[nod]=dist[nods]+cost;
                q.push(nod);
                if(nod==y) return;
            }
        }
    }
}
int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    citire();
    bfs();
    printf("%d",dist[y]-1);
    return 0;
}