Pagini recente » Cod sursa (job #3193972) | Cod sursa (job #190681) | Cod sursa (job #268893) | Cod sursa (job #2739771) | Cod sursa (job #2805611)
#include <bits/stdc++.h>
using namespace std;
vector <pair<int,int> >h[30005];
int n,m,x,y;
int viz[30005];
ifstream f("sate.in");
ofstream g("sate.out");
int bfs(int k)
{
queue <pair <int, int> >q;
int dist;
q.push({k,0});
viz[k]=1;
while(!q.empty())
{
k=q.front().first;
dist=q.front().second;
q.pop();
for(auto e:h[k])
{
if(viz[e.first]==0)
{
viz[e.first]=1;
q.push({e.first, dist+e.second});
if(e.first==y)
return dist+e.second;
}
}
}
return 0;
}
int main()
{
int i,j,k,dist;
f>>n>>m>>x>>y;
for(k=1;k<=m;k++)
{
f>>i>>j>>dist;
if(i>j) swap(i,j);
h[i].push_back({j,dist});
h[j].push_back({i,-dist});
}
g<<bfs(x)<<'\n';
return 0;
}