Pagini recente » Cod sursa (job #2645130) | Cod sursa (job #2516646) | Cod sursa (job #1095923) | Cod sursa (job #456962) | Cod sursa (job #2447130)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("sate.in");
ofstream fout ("sate.out");
const int MAXN = 3000001;
int dp[MAXN],n,m,x,y;
struct muc {
int x,y,d;
bool operator < ( const auto & next) const {
return (y < next.y);
};
};
muc a[MAXN];
int main() {
fin >> n >> m >> x >> y;
for ( int i = 1; i <= m; ++i)
fin >> a[i].x >> a[i].y >> 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;
}
}
fout << dp[y]-1;
}