Pagini recente » Cod sursa (job #755373) | Cod sursa (job #901490) | Cod sursa (job #736696) | Cod sursa (job #382886) | Cod sursa (job #3257678)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
struct muchie
{
int v;
long long d;
};
const int NMAX = 30000;
int n, m, x, y, i, viz[NMAX + 5];
vector<muchie> g[NMAX + 5];
void bfs()
{
queue<pair<int, long long>> q;
q.push({x, 0});
viz[x] = 1;
while(!q.empty())
{
int current = q.front().first;
long long dist = q.front().second;
q.pop();
if(current == y)
{
fout << dist;
return;
}
for(i = 0; i < (int)g[current].size(); ++i)
{
if(!viz[g[current][i].v])
{
viz[g[current][i].v] = 1;
q.push({g[current][i].v, dist + g[current][i].d});
}
}
}
}
int main()
{
fin >> n >> m >> x >> y;
for(i = 1; i <= m; ++i)
{
int a, b;
long long dist;
fin >> a >> b >> dist;
if(a > b)
swap(a, b);
g[a].push_back({b, dist});
g[b].push_back({a, -dist});
}
bfs();
}