Cod sursa(job #2560659)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 28 februarie 2020 10:35:35
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pi pair<int,int>

using namespace std;

#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;

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();
		for (auto t : l[z])
			if (!viz[t.first]) {
				viz[t.first] = 1;
				ans[t.first] = ans[z] + t.second;
				q.push(t.first);
				if (t.first == y)
					return ans[y];
			}
	}
}

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;
	scan(n);
	scan(m);
	scan(x);
	scan(y);
	for (int i = 1; i <= m; i++) {
		scan(u);
		scan(v);
		scan(d);
		l[u].push_back({ v,d });
		l[v].push_back({ u,-d });
	}
	if (x == y) {
		cout << 0;
		return 0;
	}
	cout << solve(x,y);
}