Cod sursa(job #1846364)

Utilizator Tomi98Osvath Tamas Tomi98 Data 12 ianuarie 2017 16:38:35
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <deque>
#define lim 30010
#include <vector>

using namespace std;

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

struct pereche{
    int vecin, cost;
}b;

deque <int > C;

vector <pereche > v[lim];

int m, n, x, y, costMin[lim], a;

void bfs(int x, int y){
    costMin[x] = 0;
    bool ok = 1;
    int sgn, new_node, node;
    C.push_back(x);
    while (!C.empty() && ok){
        node = C.front();
        C.pop_front();
        for (int i = 0; i < int(v[node].size()); i++){
            new_node = v[node][i].vecin;
            if (new_node < node) sgn = -1;
                else sgn = 1;
            if (costMin[node] + (sgn * v[node][i].cost) < costMin[new_node]){
                costMin[new_node] = costMin[node] + sgn * v[node][i].cost;
                C.push_back(v[node][i].vecin);
                if (new_node == y) {ok = 0; break;}
            }
        }
    }
}

int main()
{
    fin >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++){
        fin >> a >> b.vecin >> b.cost;
        v[a].push_back(b);
        swap(a, b.vecin);
        v[a].push_back(b);
    }
    for (int i = 1; i <= n; i++)
        costMin[i] = 200000000;
    bfs(x, y);
    fout << costMin[y];
    return 0;
}