Pagini recente » Cod sursa (job #974240) | Cod sursa (job #677375) | Cod sursa (job #2115787) | Cod sursa (job #68535) | Cod sursa (job #3237103)
#include <bits/stdc++.h>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int nmax = 3e4;
int dist[nmax+1];
vector <pair<int, int>> graph[nmax+1];
int n, m, x, y;
void bfs()
{
queue <int> q;
q.push(x);
dist[x] = 1;
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto it:graph[nod])
{
if(dist[it.first]==0)
{
if(it.first<nod)//o ia in spate
{
dist[it.first]=dist[nod]-it.second;
q.push(it.first);
}
else
{
dist[it.first]=dist[nod]+it.second;
q.push(it.first);
}
}
}
}
}
int main()
{
int a, b, d;
in>>n>>m>>x>>y;
for(int i=1; i<=m; i++)
{
in>>a>>b>>d;
graph[a].push_back({b, d});
graph[b].push_back({a, d});
}
bfs();
out<<dist[y]-1;
}