Cod sursa(job #2558869)

Utilizator RedXtreme45Catalin RedXtreme45 Data 26 februarie 2020 20:53:18
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <vector>
#include <queue>
#define Nmax 30001

using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,in,sf,ap[Nmax],dist[Nmax];
struct NodCost{
    int nod,cost;
};
vector <NodCost> G[Nmax];
void BFS(int start)
{
    queue<int> q;
    q.push(start);
    int OK=1;
    while (!q.empty() && OK==1)
    {
        int x=q.front();
        if (x==sf)
            OK=1;
        q.pop();
        for (auto i:G[x])
        {
            if (ap[i.nod]==0)
            {
                ap[i.nod]=1;
                dist[i.nod]=dist[x]+i.cost;
                q.push(i.nod);
                if (i.nod==sf){
                 OK=0;
                 return;
                }
            }
        }
    }
}
int main()
{
    int i,a,b,c;
    fin>>n>>m>>in>>sf;
    for (i=1;i<=m;i++)
    {
        fin>>a>>b>>c;
        G[a].push_back({b,c});
        G[b].push_back({a,-c});
    }
    BFS(in);
    fout<<dist[sf];
    return 0;
}