Cod sursa(job #2812089)

Utilizator alextmAlexandru Toma alextm Data 3 decembrie 2021 21:49:25
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 30001;
const int INF = 0x3f3f3f3f;

int n, m, x, y, a, b, c;
int dp[MAXN], viz[MAXN];
vector<pair<int,int>> G[MAXN];

static inline void dfs(int x) {
  viz[x] = 1;
  for(auto node : G[x])
    if(!viz[node.first]) {
      if(node.first > x)
        dp[node.first] = min(dp[node.first], dp[x] + node.second);
      else
        dp[node.first] = min(dp[node.first], dp[x] - node.second);
      dfs(node.first);
    }
}

int main() {
  fin >> n >> m >> x >> y;
  if(x > y) swap(x, y);

  memset(dp, INF, sizeof(dp));

  dp[x] = 0;
  while(m--) {
    fin >> a >> b >> c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
    if(a == x)
      dp[b] = c;
  }

  dfs(x);

  fout << dp[y] << '\n';

  return 0;
}