Cod sursa(job #1077071)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 10 ianuarie 2014 21:02:54
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <algorithm>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

//#define FORIT(it, v) for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)

const int N = 30005;

int n, m, x, y;
int d[N];
bool vis[N];
vector <pair <int, int> > graph[N];
queue <int> q;

void read() {
    f >> n >> m >> x >> y;
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        f >> a >> b >> c;
        graph[a].push_back(make_pair(b, c));
        graph[b].push_back(make_pair(a, -c));
    }
}

void bfs(int x) {
    q.push(x);

    while (!q.empty()) {
        int node = q.front();
        for (vector <pair <int, int> >::iterator it = graph[node].begin(); it != graph[node].end(); ++it) {
            if (!vis[it->first]) {
                vis[it->first] = true;
                q.push(it->first);
                d[it->first] = d[node] + it->second;
            }
        }

        q.pop();
    }
    
    g << d[y];
}

int main() {
    read();
    bfs(x);
}