Pagini recente » Cod sursa (job #212460) | Cod sursa (job #2141728) | Cod sursa (job #606942) | Cod sursa (job #2853536) | Cod sursa (job #2275387)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int Nmax=3e4+2;
int n,m,X,Y;
bitset<Nmax>viz;
vector<pair<int,int> >L[Nmax];
queue<pair<int,int> >q;
void BFS()
{
q.push({X,0});
viz[X]=1;
pair<int,int> nod;
int dist;
while(1)
{
nod=q.front();
q.pop();
for(auto i:L[nod.first])
if(!viz[i.first])
{
viz[i.first]=1;
if(i.first>nod.first)dist=nod.second+i.second;
else dist=nod.second-i.second;
if(i.first==Y)
{
fout<<dist<<"\n";
return ;
}
q.push({i.first,dist});
}
}
}
int main()
{
fin>>n>>m>>X>>Y;
int x,y,d;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>d;
L[x].push_back({y,d});
L[y].push_back({x,d});
}
BFS();
fin.close();
fout.close();
return 0;
}