Cod sursa(job #2454272)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 7 septembrie 2019 20:30:33
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 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)
{
    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;
                q.push(to);
            }
        }
    }
    fout << d[finish];
}
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;
}