Pagini recente » Cod sursa (job #600686) | Cod sursa (job #2339493) | Cod sursa (job #84061) | Cod sursa (job #927299) | Cod sursa (job #3203278)
// #include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("sate.in");
ofstream cout("sate.out");
const int INF = 20000001;
const int NMAX = 30001;
int drum[NMAX];
vector<pair<int,int>>g[NMAX];
queue<pair<int, int>>q;
void bfs(int st)
{
drum[st] = 0;
q.push({ 0,st });
while (!q.empty())
{
pair<int, int>p = q.front();
q.pop();
for(auto x:g[p.second])
if (drum[x.second] == -1)
{
drum[x.second] = drum[p.second] + x.first;
q.push({ drum[x.second],x.second });
}
}
}
int main()
{
int n, m, x, y;
cin >> n >> m >> x >> y;
for (int i = 1; i <= n; i++)
drum[i] = -1;
for (int i = 0; i < m; i++)
{
int x, y, c;
cin >> x >> y >> c;
g[x].push_back({ c,y });
g[y].push_back({ -c,x });
}
bfs(x);
cout << drum[y];
return 0;
}