Pagini recente » Cod sursa (job #209896) | Cod sursa (job #99954) | Cod sursa (job #2571235) | Cod sursa (job #1518357) | Cod sursa (job #2524333)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const short NMAX = 30005;
vector< pair< int, int > > G[NMAX];
queue< int > Q;
int N, M, X, Y;
int D[NMAX];
char visited[NMAX];
void Citire()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fin >> N >> M >> X >> Y;
for(int i = 0; i < M; i++) {
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x, c));
}
D[X] = 0;
visited[X] = 1;
Q.push(X);
}
void BFS()
{
while(!Q.empty()) {
int currentNode = Q.front();
Q.pop();
for(auto it : G[currentNode]) {
if(!visited[it.first]) {
if(it.first > currentNode)
D[it.first] = D[currentNode] + it.second;
else
D[it.first] = D[currentNode] - it.second;
if(it.first == Y) {
fout << D[Y] << "\n";
return;
}
}
Q.push(it.first);
visited[it.first] = 1;
}
}
}
int main()
{
Citire();
BFS();
}