Cod sursa(job #2447148)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 12 august 2019 11:55:39
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#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);
}