Cod sursa(job #1655530)

Utilizator pickleVictor Andrei pickle Data 18 martie 2016 02:08:19
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
ifstream fin ("sate.in");
ofstream fout ("sate.out");

const int Nmax = 30444;

int d[Nmax];
char vis[Nmax];
vector<pii> G[Nmax];

int main() {
  int N, M, x, y, a, b, c;
  fin >> N >> M >> x >> y;
  --x, --y;
  if (x > y) swap(x, y);
  while(M--) {
    fin >> a >> b >> c;
    --a, --b;
    if (a > b) swap(a, b);
    G[a].push_back({b, c});
    G[b].push_back({a, -c});
  }

  //memset(d, -1, sizeof(int)*(N+1));
  vis[x] = 1;
  d[x] = 0;
  queue<int> Q;
  Q.push(x);
  while(!Q.empty()) {
    a = Q.front(); Q.pop();

    for(auto P: G[a]) {
      if (vis[P.first])
        continue;
      d[P.first] = d[a] + P.second;
      Q.push(P.first);
      vis[P.first] = 1;
    }
  }
  fout << d[y] << endl;

  return 0;
}