Pagini recente » Cod sursa (job #415909) | Cod sursa (job #2752978) | Cod sursa (job #682394) | Cod sursa (job #1142203) | Cod sursa (job #2465386)
#include <queue>
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 30001;
int nodes, edges, X, Y;
vector < pair <int, int> > graph[NMAX];
int dist[NMAX];
queue <int> q;
int main()
{
ifstream fin("sate.in");
ofstream fout("sate.out");
fin >> nodes >> edges >> X >> Y;
for (int i = 1;i <= edges;++i)
{
int x, y, d;
fin >> x >> y >> d;
graph[x].push_back(make_pair(y, d));
graph[y].push_back(make_pair(x, -d));
}
q.push(X);
if (X == Y)
q.pop();
while (!q.empty())
{
int node = q.front();
if (node == Y)
break;
q.pop();
for (auto &next : graph[node])
{
if (dist[next.first] == 0)
{
dist[next.first] = dist[node] + next.second;
q.push(next.first);
}
}
}
fout << dist[Y] << "\n";
fin.close();
fout.close();
return 0;
}