Pagini recente » Cod sursa (job #50763) | Cod sursa (job #1209923) | Cod sursa (job #1862822) | Cod sursa (job #801230) | Cod sursa (job #3177305)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
std::ifstream fin("sate.in");
std::ofstream fout("sate.out");
const int N = 250;
int n, m, x, y;
int cost[N];
std::vector<std::vector<std::pair<int, int>>> graf;
std::queue<int> q;
std::bitset<N> viz;
void read() {
int u, v, w;
for(int i = 0; i < m; ++i) {
fin >> u >> v >> w;
u--, v--;
graf[u].push_back(std::make_pair(v, w));
graf[v].push_back(std::make_pair(u, w));
}
}
void bfs(int nod) {
viz[nod] = 1;
q.push(nod);
while(!q.empty()) {
nod = q.front();
q.pop();
for(std::pair<int, int> next : graf[nod]) {
if(!viz[next.first]) {
viz[next.first] = 1;
q.push(next.first);
if(next.first > nod)
cost[next.first] = cost[nod] + next.second;
else
cost[next.first] = cost[nod] - next.second;
}
}
}
}
int main() {
fin >> n >> m >> x >> y;
x--, y--;
graf.assign(n, std::vector<std::pair<int, int>>());
read();
bfs(x);
fout << cost[y];
return 0;
}