Cod sursa(job #2029541)

Utilizator raulrusu99Raul Rusu raulrusu99 Data 30 septembrie 2017 11:27:09
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define MAXN 30005
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector <pair <int, int> > G[MAXN];
queue

<int> Q;
int n, m, dist[MAXN];
bool viz[MAXN];

void bfs(int start)
{
    viz[start] = 1;
    dist[start] = 0;
    Q.push(start);
    while(!Q.empty())
    {
        int nod = Q.front();
        Q.pop();
        for(auto it:G[nod])
        {
            if(!viz[it.first])
            {
                viz[it.first] = 1;
                if(nod < it.first)
                {
                    Q.push(it.first);
                    dist[it.first] = dist[nod] + it.second;
                }
                else
                {
                    Q.push(it.first);
                    dist[it.first] = dist[nod] - it.second;
                }
            }

        }
    }
}

int main()
{
    int n, m, X, Y, d, x, y;
    f >> n >> m >> X >> Y;
    for(int i = 1; i <= m; i++)
    {
        f >> x >> y >> d;
        G[x].push_back(make_pair(y, d));
        G[y].push_back(make_pair(x, d));
    }
    bfs(X);

    g << dist[Y];
    return 0;
}