Pagini recente » Cod sursa (job #3154460) | Cod sursa (job #933898) | Cod sursa (job #2489227)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAX = 30005;
const int INF = 2e7+1;
int N, M, X, Y, d[MAX];
vector < pair <int, int> > A[MAX];
queue < pair <int, int> > q;
pair <int, int> sat, i;
void solve() {
for(int i = 1; i <= N; ++i)
d[i] = INF;
d[X] = 0;
q.push({X, 0});
while(!q.empty()) {
sat = q.front();
q.pop();
for(unsigned int j = 0; j < A[sat.first].size(); ++j) {
i = A[sat.first][j];
if(d[sat.first] + i.second < d[i.first])
d[i.first] = d[sat.first] + i.second;
if(i.first == Y) {
fout << d[Y];
return;
}
q.push({i.first, d[i.first]});
}
}
}
int main() {
fin >> N >> M >> X >> Y;
int a, b, d;
while(M--) {
fin >> a >> b >> d;
A[a].push_back({b, d});
A[b].push_back({a, -d});
}
solve();
}