Cod sursa(job #2560641)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 28 februarie 2020 10:25:36
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pi pair<int,int>
using namespace std;

const int nmax = 3e4 + 5;

vector<pi>l[nmax];
queue<int>q;
bool viz[nmax];
int ans[nmax];

int solve(int x, int y) {
	q.push(x);
	viz[x] = 1;
	while (!q.empty()) {
		int z = q.front();
		q.pop();
		if (z == y)
			return ans[y];
		for (auto t : l[z])
			if (!viz[t.first]) {
				viz[t.first] = 1;
				ans[t.first] = ans[z] + t.second;
				q.push(t.first);
			}
	}
}

int main()
{
	ifstream cin("sate.in");
	ofstream cout("sate.out");
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m, x, y, u, v, d;
	cin >> n >> m >> x >> y;
	for (int i = 1; i <= m; i++) {
		cin >> u >> v >> d;
		l[u].push_back({ v,d });
		l[v].push_back({ u,-d });
	}
	cout << solve(x,y);
}