Pagini recente » Cod sursa (job #2910957) | Cod sursa (job #407699) | Cod sursa (job #2469761) | Cod sursa (job #2366068) | Cod sursa (job #2956742)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int NMAX=30001;
struct node
{
int vf;
int cost;
};
vector <node> graf[NMAX];
queue <int> q;
int d[NMAX];
int main()
{
int n,m,x,y;
in>>n>>m>>x>>y;
int x1,y1;
int cost;
for(int i=1; i<=m; i++)
{
in>>x1>>y1>>cost;
graf[x1].push_back({y1,cost});
graf[y1].push_back({x1,-cost});
}
for(int i=1; i<=n; i++)
d[i]=-1;
d[x]=0;
q.push(x);
int nod;
while(!q.empty())
{
nod=q.front();
q.pop();
for(unsigned int j=0; j<graf[nod].size(); j++)
{
int r=graf[nod][j].vf;
if(d[r]==-1 || d[r]>d[nod]+graf[nod][j].cost)
{
d[r]=d[nod]+graf[nod][j].cost;
q.push(r);
}
}
}
out<<d[y];
return 0;
}