Cod sursa(job #1054204)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 13 decembrie 2013 14:58:29
Problema Sate Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.25 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int NMAX = 30001;

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

int Dist[NMAX];
bool Queued[NMAX];

class cmp
{
    public:
    inline bool operator()(const int&a,const int&b){return Dist[a]>Dist[b];}
};

int findMinimalPath(int source, int destination){

    int x, y, c;

    for(x = 1; x < NMAX; x++)
        Dist[x] = NMAX;

    priority_queue<int,vector<int>,cmp> Q;
    Dist[source] = 1;
    Q.push(source);


    while(!Q.empty()){
        x = Q.top();
        Q.pop();
        Queued[x] = false;

        for(int i = 0; i < V[x].size(); i++){
            y = V[x][i].first;
            c = V[x][i].second;

            if(x > y)
                c = -c;

            if(Dist[y] > Dist[x] + c || Dist[y] == 0){
                Dist[y] = Dist[x] + c;
                //if(!Queued[y])
                    Q.push(y),Queued[y] = true;
            }
        }
    }

    return Dist[destination] -1;
}

int main()
{
    int x, y, c, X, Y, M, N;
    in >> N >> M >> X >> Y;

    while(M--){
        in >> x >> y >> c;

        V[x].push_back(make_pair(y,c));
        V[y].push_back(make_pair(x,c));
    }

    out << findMinimalPath(X,Y);

    return 0;
}