Pagini recente » Cod sursa (job #3349747) | Cod sursa (job #1189041) | Cod sursa (job #3300127) | Diferente pentru problema/eqs intre reviziile 6 si 4 | Cod sursa (job #3305884)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector<pair<int,int>> mat[30001];
int dist[30001];
void bfs(int x)
{
queue<int> Q;
dist[x]=1;
Q.push(x);
while(!Q.empty())
{
int i=Q.front();
Q.pop();
for(auto it:mat[i])
{
if(dist[it.first]==0)
{
dist[it.first]=dist[i]+it.second;
Q.push(it.first);
}
}
}
}
int main()
{
int n,m,x,y,a,b,d;
fin>>n>>m>>x>>y;
for(int i=0;i<m;i++)
{
fin>>a>>b>>d;
if(a>b) swap(a,b);
mat[a].push_back({b,d});
mat[b].push_back({a,-d});
}
bfs(x);
fout<<dist[y]-1;
return 0;
}