Pagini recente » Cod sursa (job #2079673) | Istoria paginii runda/a | Cod sursa (job #2801259) | Cod sursa (job #174919) | Cod sursa (job #1726698)
#include <fstream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define NMAX 300005
using namespace std;
ifstream f ("sate.in");
ofstream g ("sate.out");
int n , m , st , en , x , y , cst;
int dp[NMAX];
bool c[NMAX];
vector <vector <pair <int , int> > > G;
queue <int> coada;
void bfs(int node);
int main() {
f >> n >> m >> st >> en;
G.resize(n + 5);
for (int i = 1; i <= m; ++i) {
f >> x >> y >> cst;
G[x].push_back(make_pair(y , cst));
G[y].push_back(make_pair(x , cst));
}
bfs(st);
g << dp[en];
return 0;
}
void bfs(int node) {
coada.push(node);
while (!coada.empty()) {
int node = coada.front();
coada.pop();
c[node] = 1;
int nr = G[node].size();
for (int i = 0; i < nr; ++i) {
int it = G[node][i].first;
int cost = G[node][i].second;
if (c[it])
continue;
if (it > node) {
dp[it] = dp[node] + cost;
}
else {
dp[it] = dp[node] - cost;
}
coada.push(it);
}
}
}