Cod sursa(job #2530930)

Utilizator CristiVintilacristian vintila CristiVintila Data 25 ianuarie 2020 14:27:44
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 30005
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
 
int n, m, x, y, sum, viz[NMAX];
vector< pair<int, int> > gr[NMAX];
queue<int> q;

void citire() {
  fin >> n >> m >> x >> y;
  for (int i = 1; i <= m; i++) {
    int a, b, cost;
    fin >> a >> b >> cost;
    gr[a].push_back(make_pair(b, cost));
    gr[b].push_back(make_pair(a, -1 * cost));
  }
}
 
void BFS(int n_start, int n_end) {
  q.push(n_start);
  viz[n_start] = 0;
  while (!q.empty()) {
    int nodC = q.front();
    q.pop();
    for (int i = 0; i < gr[nodC].size(); i++) {
      if (!viz[gr[nodC][i].first]) {
        viz[gr[nodC][i].first] = viz[nodC] + gr[nodC][i].second;
        q.push(gr[nodC][i].first);
        if (gr[nodC][i].first == n_end) {
          fout << viz[n_end];
          return;
        }
      }
    }
  }
}
 
int main(int argc, const char * argv[]) {
  citire();
  BFS(x, y);
}