Cod sursa(job #2454281)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 7 septembrie 2019 20:45:53
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

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

vector < pair < int, int > > a[30005];

void bfs(int k, int finish)
{
    if(k == finish) {
        fout << "0";
        exit(0);
    }
    vector < bool > f(30005);
    vector < int > d(30005);
    queue < int > q;
    q.push(k);
    f[k] = true;
    while(!q.empty()) {
        int x = q.front();
        q.pop();
        for(auto v : a[x]) {
            int to = v.first;
            int w = v.second;
            if(!f[to]) {
                f[to] = true;
                d[to] = d[x] + w;
                if(to == finish) {
                    fout << d[to];
                    exit(0);
                }
                q.push(to);
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
    int n, m, f, l;
    fin >> n >> m >> f >> l;
    for(int i = 1; i <= m; ++i) {
        int x, y, w;
        fin >> x >> y >> w;
        a[x].push_back({y, w});
        a[y].push_back({x, -w});
    }
    bfs(f, l);
    return 0;
}