Cod sursa(job #1478158)

Utilizator ZenusTudor Costin Razvan Zenus Data 28 august 2015 01:05:19
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 30000 + 10;

int N , M , b , e , i , x , y , c;
vector < pair < int , int > > g[NMAX];
int d[NMAX];
queue < int > iq;

int main()
{

fin >> N >> M >> b >> e;

for (i = 1 ; i <= M ; ++i)
{
    fin >> x >> y >> c;
    g[x].push_back({y , c});
    g[y].push_back({x , c});
}

iq.push(b);

while (iq.size())
{
    c = iq.front();
    iq.pop();

    for (auto j = g[c].begin() ; j != g[c].end() ; ++j)
    {
        if (d[j -> first]) continue;
        d[j -> first] = (j -> first > c) ? (d[c] + j -> second) : (d[c] - j -> second);

        iq.push(j -> first);
    }
}

fout << d[e] << '\n';

return 0;
}