Cod sursa(job #2858208)

Utilizator iraresmihaiiordache rares mihai iraresmihai Data 27 februarie 2022 11:11:52
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <vector>

#define MAXN 30000

using namespace std;

struct edge{
  int pos, cost;
};

struct nodes{
  int dist;
  vector <edge> edges;
  bool visited;
};

nodes graph[MAXN + 1];

static inline void addEdge(int a, int b, int cost) {
  graph[a].edges.push_back({b, cost});
  graph[b].edges.push_back({a, cost});
}

void dfs(int pos) {
  graph[pos].visited = true;

  for ( edge node : graph[pos].edges ) {
    if ( !graph[node.pos].visited ) {
      if ( node.pos < pos ) {
        graph[node.pos].dist = graph[pos].dist - node.cost;
        dfs(node.pos);
      } else {
        graph[node.pos].dist = graph[pos].dist + node.cost;
        dfs(node.pos);
      }
    }
  }

}

int main() {
  FILE *fin, *fout;
  fin = fopen("sate.in", "r");
  fout = fopen("sate.out", "w");

  int n, m, a, b, c, i, start, finish;

  fscanf(fin, "%d%d%d%d", &n, &m, &start, &finish);

  for ( i = 0; i < m; i++ ) {
    fscanf(fin, "%d%d%d", &a, &b, &c);

    addEdge(a, b, c);
  }

  dfs(start);

  fprintf(fout, "%d\n", graph[finish].dist);

  fclose(fin);
  fclose(fout);
  return 0;
}