Cod sursa(job #2560661)

Utilizator mircearoataMircea Roata Palade mircearoata Data 28 februarie 2020 10:36:54
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#pragma GCC optimize("O3")

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int n, m, x, y;
struct connection {
	int to;
	int val;
};
vector<connection> G[30005];

struct state {
	int node;
	int dist;
};
bool visited[30005];

int main() {
	ios_base::sync_with_stdio(false);
	in.tie(0);
	out.tie(0);
	in >> n >> m >> x >> y;
	for (int i = 1; i <= m; i++) {
		int a, b, d;
		in >> a >> b >> d;
		G[a].push_back({ b, d });
		G[b].push_back({ a, -d });
	}
	queue<state> q;
	q.push({ x, 0 });
	visited[x] = 1;
	while (!q.empty()) {
		state current = q.front();
		if (current.node == y) {
			out << current.dist;
			return 0;
		}
		q.pop();
		for (auto edge : G[current.node]) {
			state newState = { edge.to, current.dist + edge.val };
			visited[newState.node] = 1;
			q.push(newState);
		}
	}
}