Cod sursa(job #1503933)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 17 octombrie 2015 09:23:40
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <iostream>
#include <vector>
#include <string.h>
#include <fstream>
#include <queue>

using namespace std;

const int maxn = 100005;

int n, m, x, y, dist[maxn];
vector<pair<int, int> > g[maxn];

int main()
{
    ifstream fin("sate.in");
    ofstream fout("sate.out");
    fin >> n >> m >> x >> y;
    for(int i = 1 ; i <= m ; ++ i) {
        int x, y, z;
        fin >> x >> y >> z;
        g[x].push_back(make_pair(y, z));
        g[y].push_back(make_pair(x,-z));
    }
    memset(dist, -1, sizeof(dist));
    queue <int> q;
    q.push(x);
    dist[x] = 0;
    while(!q.empty()) {
        int node = q.front();
        q.pop();
        for(auto el : g[node]) {
            if(dist[el.first] == -1) {
                dist[el.first] = dist[node] + el.second;
                q.push(el.first);
            }
        }
    }
    fout << dist[y] << '\n';
    return 0;
}