Cod sursa(job #1059585)

Utilizator GaborGabrielFMI - GabrielG GaborGabriel Data 16 decembrie 2013 19:57:26
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
 
ifstream f("sate.in");
ofstream g("sate.out");
 
#define MAXN 30005
int N, M, X, Y, viz[MAXN];
vector<pair<int, int> > graph[MAXN];
queue<pair<int, int>> q;
 
int main()
{
    int i, a, b, c;
     
    f >> N >> M >> X >> Y;
    for (i = 1; i <= M; ++i)
        f >> a >> b >> c, graph[a].push_back(make_pair(b,c)), graph[b].push_back(make_pair(a,c));
 
    viz[X] = 1;
    q.push(make_pair(X, 0));
    while (!q.empty())
    {
        a = q.front().first;
        b = q.front().second;
        q.pop();
        for (i = 0; i < graph[a].size(); ++i)
        if (!viz[graph[a][i].first])
        {
            viz[graph[a][i].first] = 1;
            if (graph[a][i].first == Y) {
                if (graph[a][i].first > a) g << b + graph[a][i].second << '\n';
                else g << b - graph[a][i].second << '\n';
                return 0;
            }
            if (graph[a][i].first > a) q.push(make_pair(graph[a][i].first, b + graph[a][i].second));
            else q.push(make_pair(graph[a][i].first, b - graph[a][i].second));
        }
    }
 
    return 0;
}