Pagini recente » Cod sursa (job #1595842) | Cod sursa (job #2040516) | Cod sursa (job #3156070) | Cod sursa (job #1564585) | Cod sursa (job #1694077)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,x,y,x1,y1,d,used[30001],dist[30001];
vector < pair<int,int> > graf[30001];
queue <int> q;
void bfs()
{
while(!q.empty())
{
int k=q.front();
q.pop();
for(int it=0;it<graf[k].size();it++)
{
if(!used[graf[k][it].first])
{
used[graf[k][it].first]=1;
dist[graf[k][it].first]=dist[k]+graf[k][it].second;
q.push(graf[k][it].first);
}
}
}
}
int main()
{
f>>n>>m>>x>>y;
for(int i=1;i<=m;i++)
{
f>>x1>>y1>>d;
graf[x1].push_back(make_pair(y1,d));
graf[y1].push_back(make_pair(x1,-d));
}
used[x]=1;
q.push(x);
bfs();
g<<dist[y];
return 0;
}