Pagini recente » Cod sursa (job #1532729) | Cod sursa (job #3237152) | Cod sursa (job #3290052) | Cod sursa (job #3280015) | Cod sursa (job #3237137)
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
ifstream cin("sate.in");
ofstream cout("sate.out");
int n, m;
int x, y;
int a, b, c;
struct muchie {
int vecin;
int cost;
};
vector<vector<muchie>> A;
vector<bool> fr;
int main()
{
cin >> n >> m;
cin >> x >> y;
A.resize(n + 1);
fr.resize(n + 1);
for (int i = 0; i < m; i++)
{
cin >> a >> b >> c;
if (a > b)
swap(a, b);
A[a].push_back({ b,c });
A[b].push_back({ a,-c });
}
queue<pair<int,int>> Q;
Q.push({ x,0 });
fr[x] = 1;
while (!Q.empty())
{
int cap = Q.front().first;
int cost = Q.front().second;
Q.pop();
if (cap == y)
{
cout << cost;
exit(0);
}
for (int i = 0; i < A[cap].size(); i++)
{
int vecin = A[cap][i].vecin;
int new_cost = A[cap][i].cost + cost;
if (!fr[vecin])
{
fr[vecin] = 1;
Q.push({ vecin,new_cost });
}
}
}
return 0;
}