Cod sursa(job #1579845)

Utilizator tc_iuresiures tudor-cristian tc_iures Data 25 ianuarie 2016 09:39:11
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>

using namespace std;

const int Nmax = 30005;
const int INF  = 2000000000;

vector<pair<int,int> > G[Nmax];
int N, M, X, Y;
int dist[Nmax];

void read()
{
    ifstream f("sate.in");
    f >> N >> M >> X >> Y;
    for(int i = 0; i < M; i ++)
    {
        int x, y, z;
        f >> x >> y >> z;
        G[x].push_back(make_pair(y,z));
        G[y].push_back(make_pair(x,z));
    }
    f.close();
}

void BFS(int k)
{
    queue<int> Q;
    Q.push(k);
    for(int i = 1; i <= N; i ++)
    {
        dist[i] = INF;
    }
    dist[k] = 0;
    while(!Q.empty())
    {
        int Node = Q.front();
        Q.pop();
        for(int i = 0; i < G[Node].size(); i ++)
        {
            int Ngh = G[Node][i].first;
            int dst = G[Node][i].second;
            if(dist[Ngh] == INF)
            {
                if(Ngh > Node)
                {
                    dist[Ngh] = dist[Node] + dst;
                }
                else
                {
                    dist[Ngh] = dist[Node] - dst;
                }
                Q.push(Ngh);
            }
        }
    }
}

void print()
{
    ofstream g("sate.out");
    g << dist[Y];
    g.close();
}

int main()
{
    read();
    BFS(X);
    print();
    return 0;
}