Pagini recente » Cod sursa (job #359499) | Cod sursa (job #3166142) | Cod sursa (job #2315838) | Cod sursa (job #1629536) | Cod sursa (job #2465381)
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 30005;
int nodes, edges, X, Y;
vector < pair <int, int> > graph[NMAX];
int dist[NMAX];
queue <int> q;
int main()
{
FILE *fin = fopen("sate.in", "r");
FILE *fout = fopen("sate.out", "w");
fscanf(fin, "%d%d%d%d", &nodes, &edges, &X, &Y);
for (int i = 1;i <= edges;++i)
{
int x, y, d;
fscanf(fin, "%d%d%d", &x, &y, &d);
graph[x].push_back(make_pair(y, d));
graph[y].push_back(make_pair(x, d));
}
q.push(X);
while (!q.empty())
{
int node = q.front();
if (node == Y)
break;
q.pop();
for (auto &next : graph[node])
{
if (dist[next.first] == 0)
{
if (next.first > node)
dist[next.first] = dist[node] + next.second;
else
dist[next.first] = dist[node] - next.second;
q.push(next.first);
}
}
}
fprintf(fout, "%d\n", dist[Y]);
return 0;
}