Pagini recente » Cod sursa (job #2497653) | Cod sursa (job #1433199) | Cod sursa (job #1654062) | Cod sursa (job #2934184) | Cod sursa (job #2447148)
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100001;
int dp[MAXN],n,m,x,y;
const int Lim = 31;
int u = Lim - 1;
char s[Lim];
void Next () {
if (++u == Lim)
std::fread(s, 1, Lim, stdin), u = 0;
}
void Get (int &x) {
x = 0;
for (; s[u] < '0' || s[u] > '9'; Next());
for (x = 0; s[u] >= '0' && s[u] <= '9'; Next())
x = x * 10 + s[u] - '0';
}
struct muc {
int x,y,d;
bool operator < ( const auto & next) const {
return (y < next.y);
};
};
muc a[MAXN];
int main() {
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
Get(n),Get(m),Get(x),Get(y);
for ( int i = 1; i <= m; ++i)
Get( a[i].x),Get(a[i].y),Get(a[i].d);
//sort(a + 1, a+ 1 + m);
dp[x] = 1;
while ( !dp[y]) {
for ( int i = 1; i <= m; ++i)
if (!dp[a[i].y] and dp[a[i].x])
dp[a[i].y] = dp[a[i].x]+ a[i].d;
else
if (!dp[a[i].x] and dp[a[i].y]) {
dp[a[i].x] = dp[a[i].y] - a[i].d;
}
}
printf("%d", dp[y]-1);
}