Pagini recente » Junior Challenge 2016 Runda 2 | Cod sursa (job #2197175) | Statisticile problemei Manhattan | Cod sursa (job #880802) | Cod sursa (job #3299725)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100024;
int n, m;
vector<pair<int, int>> adj[NMAX];
int dist[NMAX];
bool viz[NMAX];
int bfs(int start, int target)
{
queue<int> q;
q.push(start);
viz[start] = true;
dist[start] = 0;
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto vecin : adj[nod])
{
int next = vecin.first;
int d = vecin.second;
if (!viz[next])
{
viz[next] = true;
if (nod < next)
dist[next] = dist[nod] + d;
else
dist[next] = dist[nod] - d;
if (next == target)
return dist[next];
q.push(next);
}
}
}
return -1;
}
int main()
{
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
int x, y;
scanf("%d %d %d %d", &n, &m, &x, &y);
for (int i = 0; i < m; i++)
{
int a, b, d;
scanf("%d %d %d", &a, &b, &d);
adj[a].push_back({b, d});
adj[b].push_back({a, d});
}
printf("%d\n", bfs(x, y));
return 0;
}