Pagini recente » Cod sursa (job #2300045) | Rating Blinda Alexandru (Tarnacop) | Monitorul de evaluare | Cod sursa (job #2686744) | Cod sursa (job #3239247)
#include<fstream>
#include<vector>
#include<queue>
#include<bitset>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
const int NMAX=30005;
std::vector<std::pair<int, int>>G[NMAX];//dist, to
std::bitset<NMAX>f;
int n, m, x, y;
void read()
{
fin>>n>>m>>x>>y;
for(int i=0; i<m; ++i)
{
int from, to, dist;
fin>>from>>to>>dist;
G[from].emplace_back(dist, to);
G[to].emplace_back(dist, from);
}
}
int bfs()
{
std::queue<std::pair<int, int>>q;//current_value, node
q.emplace(0, x);
f[x]=true;
while(!q.empty())
{
std::pair<int, int>local=q.front();
q.pop();
for(auto it:G[local.second])
{
if(!f[it.second])
{
f[it.second]=true;
int newCost=local.first;
if(it.second<local.second)
newCost-=it.first;
else
newCost+=it.first;
if(it.second==y)
return newCost;
q.emplace(newCost, it.second);
}
}
}
return -1;
}
int main()
{
std::ios_base::sync_with_stdio(false);
read();
fout<<bfs();
return 0;
}