Pagini recente » Cod sursa (job #1727795) | Cod sursa (job #693362) | Cod sursa (job #781951) | Cod sursa (job #1741739) | Cod sursa (job #1505701)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 30001, INF = 20000001;
vector < pair <int, int> > t[MAX];
queue <int> q;
int n, m, x, y, d[MAX];
ifstream cin("sate.in");
ofstream cout("sate.out");
int main()
{
//freopen("sate.in","r",stdin);
//freopen("sate.out","w",stdout);
//scanf("%d%d%d%d", &n, &m, &x, &y);
cin>>n>>m>>x>>y;
for(register int i=1; i<=m; i++)
{
int a, b, d;
//scanf("%d%d%d", &a, &b, &d);
cin>>a>>b>>d;
t[a].push_back(make_pair(b, d));
t[b].push_back({a, -d});
}
q.push(x);
for(register int i=1;i<=n;++i)
d[i]=INF;
d[x] = 0;
while(!q.empty()){
int id = q.front();
q.pop();
for(register unsigned i=0; i<t[id].size(); i++)
{
int nextid = t[id][i].first;
int dist = t[id][i].second;
if(d[nextid]==INF)
{
d[nextid] = d[id] + dist;
q.push(nextid);
}
if(nextid==y)
{
cout<<d[y];
return 0;
}
}
}
//printf("%d", d[y]);
cout<<d[y];
return 0;
}