Pagini recente » Cod sursa (job #3262738) | Cod sursa (job #919248) | Cod sursa (job #1531081) | Cod sursa (job #2345883) | Cod sursa (job #3237756)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 30005;
vector<pair<int,int>> graph[MAX];
vector<int> dist;
void bfs(int start)
{
queue<int> q;
q.push(start);
dist[start] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
for (pair<int,int> next : graph[node])
{
if (!dist[next.first])
{
dist[next.first] = dist[node] + next.second;
q.push(next.first);
}
}
}
}
int main()
{
ifstream cin("sate.in");
ofstream cout("sate.out");
int n, m, x, y;
cin >> n >> m >> x >> y;
dist.assign(n+1, 0);
for (int i = 1; i <= m; ++i)
{
int x, y, cost;
cin >> x >> y >> cost;
graph[x].push_back({y, cost});
graph[y].push_back({x, -cost});
}
bfs(x);
cout << dist[y]-1;
return 0;
}