Pagini recente » Cod sursa (job #2377726) | Cod sursa (job #601410) | Cod sursa (job #2549926) | Cod sursa (job #3216507) | Cod sursa (job #1396839)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
using namespace std;
fstream fin("sate.in", ios::in);
fstream fout("sate.out", ios::out);
#define MAXDIM 30005
pair <int, int> exe;
vector <pair<int, int>> list[MAXDIM];
int final_dist;
bool used[MAXDIM];
void read()
{
int x, y, m, c, n;
fin >> n >> m >> x >> y;
exe.first = x; exe.second = y;
for (int i = 1; i <= m; i++){
fin >> x >> y >> c,
list[x].push_back(make_pair(y, c)),
list[y].push_back(make_pair(x, c));
}
fin.close();
}
void BFS()
{
queue <pair<int, int>> q;
q.push(make_pair(exe.first, 0));
while (!q.empty()){
pair <int, int> node = q.front(), new_node;
q.pop();
if (node.first == exe.second) { final_dist = node.second; return; }
used[node.first] = true;
for (int i = 0, size = list[node.first].size(); i < size; i++){
new_node = list[node.first][i];
if (used[new_node.first]) continue;
q.push(make_pair(new_node.first, new_node.first > node.first ? node.second + new_node.second : node.second - new_node.second));
}
}
}
int main()
{
read();
BFS();
fout << final_dist << "\n";
fout.close();
return 0;
}