Pagini recente » Cod sursa (job #2228945) | Cod sursa (job #15079) | Cod sursa (job #3201217) | Cod sursa (job #7302) | Cod sursa (job #3201058)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("sate.in");
ofstream cout("sate.out");
int n,m;
int start,final;
int x,y;
long long c;
vector<vector<pair<int,int> > > A;
vector<bool> fr;
void bfs()
{
queue<pair<int,long long> > Q;
Q.push({start,0});
fr[start]=1;
while(!Q.empty())
{
int cap=Q.front().first;
int cost=Q.front().second;
Q.pop();
if(cap==final)
{
cout<<cost;
exit(0);
}
for(int i=0;i<A[cap].size();i++)
{
int vecin=A[cap][i].first;
long long new_cost=A[cap][i].second;
if(!fr[vecin])
{
fr[vecin]=1;
if(cap<vecin)
Q.push({vecin,cost+new_cost});
else
Q.push({vecin,cost-new_cost});
}
}
}
}
int main()
{
cin>>n>>m;
cin>>start>>final;
A.resize(n+1);
fr.resize(n+1);
for(int i=0;i<m;i++)
{
cin>>x>>y>>c;
A[x].push_back({y,c});
A[y].push_back({x,c});
}
bfs();
return 0;
}