Pagini recente » Cod sursa (job #2575247) | Cod sursa (job #2489741) | Cod sursa (job #262078) | Cod sursa (job #898599) | Cod sursa (job #1726703)
#include <fstream>
#include <vector>
#include <cstdio>
#include <queue>
#include <map>
#include <set>
#define NMAX 30005
using namespace std;
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);
void read(int &x) {
int sign = 1;
char ch;
x = 0;
while (!isdigit(ch = getchar())) {
if (ch == '-') {
sign = -1;
}
else
sign = 1;
}
do {
x = x * 10 + ch - '0';
} while (isdigit(ch = getchar()));
x *= sign;
}
int main() {
freopen("sate.in" , "r" , stdin);
freopen("sate.out" ,"w" , stdout);
read(n) , read(m) , read(st) , read(en);
G.resize(n + 5);
for (int i = 1; i <= m; ++i) {
read(x) , read(y) , read(cst);
G[x].push_back(make_pair(y , cst));
G[y].push_back(make_pair(x , -cst));
}
bfs(st);
printf("%d" , dp[en]);
return 0;
}
void bfs(int node) {
coada.push(node);
c[node] = 1;
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] == 0) {
dp[it] = dp[node] + cost;
coada.push(it);
}
}
}
}