Pagini recente » Cod sursa (job #2371431) | Cod sursa (job #376541) | Cod sursa (job #2720944) | Cod sursa (job #922882) | Cod sursa (job #2497010)
#include <bits/stdc++.h>
#define Nmax 30005
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n, m, dist[Nmax], start, fin, pos, viz[Nmax];
vector <pair <int, int> > G[Nmax];
void read()
{
f >> n >> m >> start >> fin;
int x, y, c;
for (int i = 1; i <= m; ++i) {
f >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, -c});
}
}
void bfs(int start)
{
queue <int> Coada;
Coada.push(start);
dist[start] = 0;
viz[start] = 1;
while (!Coada.empty()) {
int node = Coada.front();
Coada.pop();
int length = G[node].size();
for (int pos = 0; pos < length; ++pos) {
int newNode = G[node][pos].first;
int newCost = G[node][pos].second;
if (!viz[newNode]) {
viz[newNode] = 1;
dist[newNode] = dist[node] + newCost;
if (newNode == fin)
break;
Coada.push(newNode);
}
}
}
g << dist[fin];
}
int main()
{
ios_base::sync_with_stdio(false);
read();
bfs(start);
return 0;
}