Cod sursa(job #2497752)

Utilizator matei123Biciusca Matei matei123 Data 23 noiembrie 2019 10:53:31
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <vector>
#include <queue>
#define Nmax 30005
using namespace std;
ifstream f("sate.in"); ofstream g("sate.out");
vector <pair<int,int> >G[Nmax];
queue <int > Q;
int n,m,alfa,omega,l[Nmax];
void citire()
{   f>>n>>m>>alfa>>omega;
    for(int i=1; i<=m; i++)
    {   int x,y,d;
        f>>x>>y>>d;
        G[x].push_back({y,d});
        G[y].push_back({x,d});
    }
}
void bfs()
{   while(!Q.empty())
    {   int x=Q.front();
        Q.pop();
        for(auto &v: G[x])
        {   if(l[v.first]==0)
            {   Q.push(v.first);
                if(v.first<x) l[v.first]=l[x]-v.second;
                else l[v.first]=l[x]+v.second;
            }
        }
    }
}
int main()
{   citire();
    if(alfa>omega) swap(alfa,omega);
    Q.push(alfa);
    bfs();
    g<<l[omega];
    return 0;
}