Cod sursa(job #2118432)

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

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

void dfs(int x, int c)
{
	rez[x] = c;
	viz[x] = 1;
	for (int 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");
	int n, m, x, y;
	cin >> n >> m >> x >> y;
	for (int i = 0; i < m; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		pair<int, int> 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];
}