Pagini recente » Cod sursa (job #2852179) | Cod sursa (job #3040763) | Cod sursa (job #2255899) | Cod sursa (job #2259844) | Cod sursa (job #2558878)
#include <fstream>
#include <vector>
#include <queue>
#define Nmax 30001
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,in,sf,dist[Nmax];
bool ap[Nmax];
vector <pair<int,int> > G[Nmax];
queue<int> q;
int BFS(int start)
{
q.push(start);
while (!q.empty())
{
int x=q.front();
q.pop();
for (auto i:G[x])
{
if (ap[i.first]==0)
{
ap[i.first]=1;
dist[i.first]=dist[x]+i.second;
q.push(i.first);
if (i.first==sf){
return dist[i.first];
}
}
}
}
}
int main()
{
int i,a,b,c;
fin>>n>>m>>in>>sf;
for (i=1;i<=m;i++)
{
fin>>a>>b>>c;
G[a].push_back({b,c});
G[b].push_back({a,-c});
}
fout<<BFS(in);
return 0;
}