Pagini recente » Cod sursa (job #1235804) | Cod sursa (job #744412) | Cod sursa (job #885569) | Cod sursa (job #3219074) | Cod sursa (job #2650844)
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <fstream>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n, m, x, y, gasit;
vector<int> ocupat(30001, 0);
struct nod
{
int d, cost;
};
vector<vector<nod> > liste(30001, vector<nod>());
void bfs(int el, int cost)
{
ocupat[el] = 1;
if (el == y)
{
fout << cost;
gasit = 1;
return;
}
for (int i = 0; i < liste[el].size(); i++)
if (ocupat[liste[el][i].d] == 0 && gasit == 0)
bfs(liste[el][i].d, cost + liste[el][i].cost);
ocupat[el] = 0;
}
int main()
{
fin >> n >> m >> x >> y;
for (int i = 1; i <= m; i++)
{
int a, b, c;
fin >> a >> b >> c;
nod x, y; x.d = b; y.d = a; x.cost = y.cost = c;
if (a > b)
x.cost *= -1;
else
y.cost *= -1;
liste[a].push_back(x);
liste[b].push_back(y);
}
bfs(x, 0);
}