Pagini recente » Cod sursa (job #906541) | Cod sursa (job #1510794) | Cod sursa (job #1627223) | Cod sursa (job #2148162) | Cod sursa (job #2496792)
#include <bits/stdc++.h>
using namespace std;
/// sat d
vector<pair<int, int> > a[30001];
queue<int> q;
int d[30003];
bitset<30003> viz;
ifstream fin("sate.in");
ofstream fout("sate.out");
int main()
{
int i, sat, X, Y, n, m, x, y, dist;
fin >> n >> m >> X >> Y;
for (i = 1; i <= m; i++)
{
fin >> x >> y >> dist;
if (x > y) swap(x, y);
a[x].push_back({y, dist});
a[y].push_back({x, -dist});
}
q.push(X);
while (!q.empty())
{
x = q.front();
q.pop();
for (auto w : a[x])
{
sat = w.first;
dist = w.second;
if (viz[sat] == 0)
{
viz[sat] = 1;
d[sat] = d[x] + dist;
q.push(sat);
if (sat == Y)
{
fout << d[Y] << "\n";
return 0;
}
}
}
}
return 0;
}