Cod sursa(job #2118396)

Utilizator petrea1551Petrea Calin petrea1551 Data 30 ianuarie 2018 16:59:00
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
	ifstream cin("sate.in");
	ofstream cout("sate.out");
	int n, m, x, y, sum = 0;
	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);
	}
	int w = min(x,y);
	y = max(x, y);
	x = w;
	q.push(x);
	while (!(q.empty()))
	{
		int c = q.front();
		q.pop();
		viz[c] = 1;
		if (c == y)
		{
			cout << sum;
			return 0;
		}
		for (int i = 0; i < a[c].size(); i++)
			if (!viz[a[c][i].first])
			{
				q.push(a[c][i].first);
				if (c < a[c][i].first)
					sum += a[c][i].second;
				else
					sum -= a[c][i].second;
			}
	} 
}