Cod sursa(job #1082800)

Utilizator poptibiPop Tiberiu poptibi Data 14 ianuarie 2014 22:10:03
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

const int NMAX = 30010;

int N, M, X, Y, C, Start, End, Dist[NMAX];
vector<pair<int, int> > G[NMAX];
queue<int> Q;

void BFS()
{
    for(int i = 1; i <= N; ++ i) Dist[i] = -1;
    Q.push(Start);
    Dist[Start] = 0;

    while(!Q.empty())
    {
        int Node = Q.front();
        Q.pop();

        for(vector<pair<int, int> > :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it)
        {
            int Vec = it -> first, Cost = it -> second;
            if(Dist[Vec] == -1)
            {
                if(Vec < Node) Dist[Vec] = Dist[Node] - Cost;
                else Dist[Vec] = Dist[Node] + Cost;
                Q.push(Vec);
            }
        }
    }

    printf("%i\n", Dist[End]);
}

int main()
{
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);

    scanf("%i %i %i %i", &N, &M, &Start, &End);
    for(int i = 1; i <= M; ++ i)
    {
        scanf("%i %i %i", &X, &Y, &C);
        G[X].push_back(make_pair(Y, C));
        G[Y].push_back(make_pair(X, C));
    }

    BFS();
}