Pagini recente » Cod sursa (job #1161021) | Cod sursa (job #1595947) | Cod sursa (job #1802071) | Cod sursa (job #1456003) | Cod sursa (job #2455710)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector < pair<int,int> > graph[30005];
queue <int> q;
int n,m,a,b,x,y,c,d[30005],i;
int main()
{
f>>n>>m>>a>>b;
while(m--)
{
f>>x>>y>>c;
graph[x].push_back({y,c});
graph[y].push_back({x,-c});
}
for(i=1;i<=n;i++)
{
d[i]=1e9;
}
if(a>b)
swap(a,b);
q.push(a);
d[a]=0;
while(!q.empty())
{
x=q.front();
if(x==b)
{
g<<d[b];
break;
}
q.pop();
for(i=0;i<(int)graph[x].size();i++)
{
if(d[graph[x][i].first]>d[x]+graph[x][i].second)
{
d[graph[x][i].first]=d[x]+graph[x][i].second;
q.push(graph[x][i].first);
}
}
}
return 0;
}