Cod sursa(job #2956742)

Utilizator andreibrosPeta Andrei Mathias andreibros Data 20 decembrie 2022 14:19:06
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int NMAX=30001;
struct node
{
    int vf;
    int cost;
};
vector <node> graf[NMAX];
queue <int> q;
int d[NMAX];
int main()
{
    int n,m,x,y;
    in>>n>>m>>x>>y;
    int x1,y1;
    int cost;
    for(int i=1; i<=m; i++)
    {
        in>>x1>>y1>>cost;
        graf[x1].push_back({y1,cost});
        graf[y1].push_back({x1,-cost});
    }
    for(int i=1; i<=n; i++)
        d[i]=-1;
    d[x]=0;
    q.push(x);
    int nod;
    while(!q.empty())
    {
        nod=q.front();
        q.pop();
        for(unsigned int j=0; j<graf[nod].size(); j++)
        {
            int r=graf[nod][j].vf;

            if(d[r]==-1 || d[r]>d[nod]+graf[nod][j].cost)
                {
                    d[r]=d[nod]+graf[nod][j].cost;
                    q.push(r);

                }

        }
    }
    out<<d[y];

    return 0;
}