Pagini recente » Cod sursa (job #1967030) | Cod sursa (job #255569) | Cod sursa (job #2512064) | Cod sursa (job #1450566) | Cod sursa (job #2497024)
#include <bits/stdc++.h>
#define Nmax 30005
using namespace std;
FILE *f = fopen("sate.in", "r");
FILE *g = fopen("sate.out", "w");
int n, m, dist[Nmax], start, fin, viz[Nmax];
vector <pair <int, int> > G[Nmax];
void read()
{
fscanf(f, "%d%d%d%d", &n, &m, &start, &fin);
int x, y, c;
for (int i = 1; i <= m; ++i) {
fscanf(f, "%d%d%d", &x, &y, &c);
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(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();
for (auto pos : G[node]) {
int newNode = pos.first;
int newCost = pos.second;
if (!viz[newNode]) {
viz[newNode] = 1;
dist[newNode] = dist[node] + newCost;
if (newNode == fin)
break;
Coada.push(newNode);
}
}
}
fprintf(g, "%d", dist[fin]);
}
int main()
{
ios_base::sync_with_stdio(false);
read();
bfs(start);
return 0;
}