Pagini recente » Cod sursa (job #2577709) | Cod sursa (job #1768260) | Cod sursa (job #266251) | Cod sursa (job #300391) | Cod sursa (job #1417551)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
int main()
{
long k,n,m,x,y,i,j,d,*distanta;
queue <long>q;
vector <pair<long, long>> *v;
ifstream f("sate.in");
ofstream g("sate.out");
f >> n >> m >> x >>y;
v=new vector<pair<long,long>>[n];
distanta=new long [n];
for (k=0;k<m;k++)
{
f>>i>>j>>d;
v[i].push_back(make_pair(j, d));
v[j].push_back(make_pair(i, -d));
}
for (i=0;i<n;i++)
{
distanta[i]=-999;
}
distanta[x]=0;
q.push(x);
while(!q.empty())
{
k=q.front();
q.pop();
for(vector< pair<long,long > >::iterator i=v[k].begin();i!=v[k].end();i++)
{
if (distanta[i->first]==-999)
{
distanta[i->first]=distanta[k]+i->second;
if (i->first==y)
{
g<<distanta[i->first];
return 0;
}
q.push(i->first);
}
}
}
}