Cod sursa(job #1882617)

Utilizator DobosDobos Paul Dobos Data 17 februarie 2017 12:50:38
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>
#define NMAX 360
#define INF 1e9
#define f first
#define s second
using namespace std;

ifstream fin("fmcm.in");
ofstream fout("fmcm.out");

vector < pair < int , int > > G[NMAX];

int F[NMAX][NMAX],D[NMAX];
bool B[NMAX];


void bellmanford(const int &n,const int &S){
    for(int i = 1; i <= n; i++)
        D[i] = INF;

    D[S] = 0;
    int nod;
    B[S] = 1;
    deque < int > Q;
    Q.push_back(S);

    while(!Q.empty()){
        nod = Q.front();
        Q.pop_front();
        B[nod] = 0;
        for(auto const &it : G[nod]){
            if(D[it.f] <= D[nod] + it.s) return;

            D[it.f] = D[nod] + it.s;
            if(!B[it.f]){
                Q.push_back(it.f);
                B[it.f] = 1;
            }

        }


    }


}

int main()
{
    ios :: sync_with_stdio(false);

    int n,m,x,y,c,f,S,D;

    fin >> n >> m >> S >> D;

    for(int i = 1; i <= m; i++){

        fin >> x >> y >> c >> f;
        G[x].push_back({y,f});
        G[y].push_back({x,f});

        F[x][y] = c;

    }

    bellmanford(n,S);

    for(int i = 1; i <= n; i++)
        fout << D[i] << " ";
    return 0;
}