Cod sursa(job #1495256)

Utilizator DobosDobos Paul Dobos Data 2 octombrie 2015 20:22:46
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX = 30005;
vector < int > G[MAX];
bitset < MAX > trec;
deque < int > lx;
int D[MAX];
inline void dfs()
{
    int x;
    while(!lx.empty()){
        x = lx.front();
        lx.pop_front();
        for(int i = 0; i < G[x].size(); i += 2){
            if(trec[G[x][i]] == 0){
                trec[G[x][i]] = 1;
                if(x < G[x][i])
                    D[G[x][i]] = D[x] + G[x][i+1];
                else
                    D[G[x][i]] = D[x] - G[x][i+1];
                lx.push_back(G[x][i]);
            }
        }
    }
}
int main()
{
    int n,m,s,f,x,y,d;
    fin >> n >> m >> s >> f;
    for(int i = 0 ; i < m; i ++){
        fin >> x >> y >> d;
        G[x].push_back(y);
        G[x].push_back(d);
        G[y].push_back(x);
        G[y].push_back(d);
    }
    lx.push_back(s);
    dfs();
    fout << max(-D[f],D[f]);

    return 0;
}