Pagini recente » Cod sursa (job #489987) | Cod sursa (job #1374218) | Cod sursa (job #1252289) | Cod sursa (job #1047964) | Cod sursa (job #1511201)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
const int nmax = 30005;
const int oo = 1<<29;
vector <pair<int,int> > g[nmax];
int dist[nmax], n;
bool viz[nmax];
void bfs(int start, int finish)
{
queue <int> coada;
int dad, son, cost, i;
for(i=1; i<=n; i++)
dist[i]=oo;
coada.push(start);
viz[start] = true;
dist[start] = 0;
while (!coada.empty())
{
dad=coada.front();
coada.pop();
for (i=0; i<g[dad].size(); i++)
{
son=g[dad][i].first;
cost=g[dad][i].second;
if (viz[son]==false && (dist[son]==oo || dist[son] > dist[dad]+cost))
{
dist[son]=dist[dad]+cost;
viz[son]=true;
coada.push(son);
}
}
}
}
int main()
{
ifstream fin ("sate.in");
ofstream fout ("sate.out");
int m, x, y, c, a, b, i;
fin >> n >> m >> x >> y;
for(i=1; i<=m; i++)
{
fin >> a >> b >> c;
g[a].push_back(make_pair(b, c));
g[b].push_back(make_pair(a, -c));
}
bfs(x, y);
fout << dist[y];
fin.close();
fout.close();
return 0;
}