Pagini recente » Cod sursa (job #2022297) | Cod sursa (job #3227440) | Cod sursa (job #200448) | Cod sursa (job #841617) | Cod sursa (job #1452679)
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
vector<vector<pair<int,int> > > a;
unsigned n,st,fi,x,y,aux;
long m,d;
long long dist[30005];
queue<unsigned> q;
void BFS()
{
for(int i = 0;i<=n;i++)
dist[i] = -1;
for(q.push(st);!q.empty();q.pop())
{
aux = q.front();
for(int i = 0;i<a[aux].size();i++)
{
if(dist[ a[aux][i].first ] == -1)
{
q.push(a[aux][i].first);
dist[a[aux][i].first] = dist[aux] + a[aux][i].second;
}
}
}
}
int main()
{
FILE * f = fopen("sate.in","r");
FILE * g = fopen("sate.out","w");
fscanf(f,"%d%d%d%d",&n,&m,&st,&fi);
a.reserve(30005);
for(int i = 0 ;i<m;i++)
{
fscanf(f,"%d%d%d",&x,&y,&d);
a[x].push_back(make_pair(y,d));
a[y].push_back(make_pair(x,-d));
}
BFS();
fprintf(g,"%lld",dist[fi]+1);
fclose(f);
fclose(g);
return 0;
}