Cod sursa(job #2855040)

Utilizator vladburacBurac Vlad vladburac Data 22 februarie 2022 02:03:05
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 3e4;
const int INF = 2e9;

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

struct Edge{
  int node, cost;
};
vector <Edge> graph[NMAX];
queue <int> bfsQueue;
int dist[NMAX];
bool marked[NMAX];
int n;

void bfs( int node ) {
  dist[node] = 0;
  bfsQueue.push( node );
  marked[node] = true;
  while( !bfsQueue.empty() ) {
    int qfront = bfsQueue.front();
    bfsQueue.pop();
    for( auto edge: graph[qfront] ) {
      if( !marked[edge.node] ) {
        bfsQueue.push( edge.node );
        marked[edge.node] = true;
        dist[edge.node] = dist[qfront] + edge.cost;
      }
    }
  }
}

int main() {
  int m, x, y, i, a, b, d;
  fin >> n >> m >> x >> y;
  x--;
  y--;
  for( i = 0; i < m; i++ ) {
    fin >> a >> b >> d;
    a--;
    b--;
    graph[a].push_back( { b, d } );
    graph[b].push_back( { a, -d } );
  }
  bfs( x );
  fout << dist[y];
  return 0;
}