Pagini recente » Cod sursa (job #2906277) | Cod sursa (job #3153240) | Cod sursa (job #1613106) | Cod sursa (job #2887859) | Cod sursa (job #3289702)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX=30000;
int n,m,x,y,i,nod1,nod2,cost,sum;
bool viz[MAX+5];
vector <pair<int,int>> muchii[MAX+5];
queue <pair<int,int>> q;
signed main()
{
fin>>n>>m>>x>>y;
while (m)
{
fin>>nod1>>nod2>>cost;
muchii[nod1].push_back({nod2,cost});
muchii[nod2].push_back({nod1,cost});
m--;
}
q.push({x,0});
while (!q.empty())
{
nod1=q.front().first;
sum=q.front().second;
if (nod1==y)
{
fout<<sum;
return 0;
}
for (auto x: muchii[nod1])
if (!viz[x.first])
{
viz[x.first]=1;
if (x.first>nod1)
q.push({x.first,sum+x.second});
else
q.push({x.first,sum-x.second});
}
q.pop();
}
fout<<-1;
return 0;
}