Pagini recente » Cod sursa (job #2543610) | Cod sursa (job #1407615) | Cod sursa (job #2122071) | Cod sursa (job #2442000) | Cod sursa (job #2583335)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
typedef pair <int, int> p;
const int NMAX = 30003;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector <p> g[NMAX];
int dist[NMAX], d;
int n, m, x, y, a, b;
bitset <NMAX> viz;
queue <int> q;
void solve()
{
while (!q.empty())
{
x = q.front();
if (x == b)
return;
viz[x] = 1;
q.pop();
for (size_t i = 0; i < g[x].size(); ++i)
{
if (viz[g[x][i].first] == 0)
{
dist[g[x][i].first] = dist[x] + g[x][i].second;
q.push(g[x][i].first);
}
}
}
}
int main()
{
fin >> n >> m >> a >> b;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y >> d;
g[x].push_back({ y, d });
g[y].push_back({ x, -d });
}
q.push(a);
solve();
fout << dist[b] << "\n";
return 0;
}