Cod sursa(job #2558878)

Utilizator RedXtreme45Catalin RedXtreme45 Data 26 februarie 2020 20:58:02
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 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,dist[Nmax];
bool ap[Nmax];
vector <pair<int,int> > G[Nmax];
queue<int> q;
int BFS(int start)
{
    q.push(start);
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        for (auto i:G[x])
        {
            if (ap[i.first]==0)
            {
                ap[i.first]=1;
                dist[i.first]=dist[x]+i.second;
                q.push(i.first);
                if (i.first==sf){
                    return dist[i.first];
                }
            }
        }
    }
}
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});
    }
    fout<<BFS(in);
    return 0;
}