Pagini recente » Cod sursa (job #2496451) | Cod sursa (job #1554058) | Cod sursa (job #3194550) | Cod sursa (job #2874043) | Cod sursa (job #3258137)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 30000;
const int INF = 21e8;
ifstream cin("sate.in");
ofstream cout("sate.out");
struct noduri {
int nod, cost;
};
vector <vector <noduri>> v;
int dist[NMAX + 2], n;
bool f[NMAX + 2]; ///dc e in coada
void bfs(int start) {
for(int i = 1; i <= n; i++)
dist[i] = INF;
dist[start] = 0;
f[start] = 1;
queue <int> q;
q.push(start);
while(!q.empty()) {
int now = q.front();
f[now] = 0;
q.pop();
for(auto var : v[now]) {
if(dist[var.nod] > dist[now] + var.cost) {
dist[var.nod] = dist[now] + var.cost;
if(!f[var.nod]) {
f[var.nod] = 1;
q.push(var.nod);
}
}
}
}
}
int main()
{
int m, x, y, a, b, c;
cin >> n >> m >> x >> y;
v.resize(n + 1);
for(int i = 1; i <= m; i++) {
cin >> a >> b >> c;
if(a > b)
swap(a, b);
v[a].push_back({b, c});
v[b].push_back({a, -c});
}
bfs(x);
cout << dist[y];
return 0;
}