Cod sursa(job #2118434)

Utilizator petrea1551Petrea Calin petrea1551 Data 30 ianuarie 2018 17:26:28
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
using namespace std;

vector<pair<long long, long long> > a[31000];
bool viz[31000];
long long rez[31000];
queue <long long> q;

void dfs(long long x, long long c)
{
	rez[x] = c;
	viz[x] = 1;
	for (long long i = 0; i < a[x].size(); i++)
		if (!viz[a[x][i].first])
			dfs(a[x][i].first, x < a[x][i].first ? c + a[x][i].second : c - a[x][i].second); 
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ifstream cin("sate.in");
	ofstream cout("sate.out");
	long long n, m, x, y;
	cin >> n >> m >> x >> y;
	for (long long i = 0; i < m; i++)
	{
		long long x, y, z;
		cin >> x >> y >> z;
		pair<long long, long long> w;
		w.first = x;
		w.second = z;
		a[y].push_back(w);
		w.first = y;
		a[x].push_back(w);
	}
	dfs(x, 0);
	cout << rez[y];
}